class Post < ActiveRecord::Base

  validates :title, :body, :user_id, presence: true

  belongs_to :user
  has_many :taggings, dependent: :destroy
  has_many :tags, through: :taggings
  has_many :responses, dependent: :destroy
  has_many :likes, as: :likeable
  has_many :likers, through: :likes, source: :user

  has_many :bookmarks, as: :bookmarkable
  has_many :bookmarkers, through: :bookmarks, source: :user

  delegate :username, to: :user

  default_scope { order(created_at: :desc) }
  scope :latest, ->(number) { order(created_at: :desc).limit(number) }
  scope :top_stories, ->(number) { order(:likes_count).limit(number) }

  mount_uploader :picture, PictureUploader

  def self.tagged_with(name)
    Tag.find_by!(name: name).posts
  end

  def all_tags=(names)
    self.tags = names.split(",").map do |name|
      Tag.where(name: name.strip).first_or_create!
    end
  end

  def all_tags
    tags.map(&:name).join(", ")
  end
end

class Post < ActiveRecord::Base

  validates :title, :body, :user_id, presence: true

  belongs_to :user
  has_many :taggings, dependent: :destroy
  has_many :tags, through: :taggings
  has_many :responses, -> { order(created_at: :desc) }, dependent: :destroy
  has_many :likes, as: :likeable
  has_many :likers, through: :likes, source: :user

  has_many :bookmarks, as: :bookmarkable
  has_many :bookmarkers, through: :bookmarks, source: :user

  delegate :username, to: :user

  default_scope { order(created_at: :desc) }
  scope :latest, ->(number) { order(created_at: :desc).limit(number) }
  scope :top_stories, ->(number) { order(:likes_count).limit(number) }

  mount_uploader :picture, PictureUploader

  def self.tagged_with(name)
    Tag.find_by!(name: name).posts
  end

  def all_tags=(names)
    self.tags = names.split(",").map do |name|
      Tag.where(name: name.strip).first_or_create!
    end
  end

  def all_tags
    tags.map(&:name).join(", ")
  end
end

Modify has_many :responses in Post model to return responses ordered by created_at in descending order
Modify has_many :responses in Post model to return responses ordered by created_at in descending order
