显示下6个帖子

时间:2015-04-04 20:09:13

标签: ruby ruby-on-rails-4 activerecord

我正在寻找创建“相关帖子”部分,但只是在我的show.html.erb上显示系列中的后6个帖子作弊

我将如何展示这些?

谢谢,

乔恩

编辑 - 添加帖子模型

class Post < ActiveRecord::Base
    has_and_belongs_to_many :categories
    belongs_to :user
    is_impressionable :counter_cache => true, :column_name => :view_count

    acts_as_taggable_on :tags

    extend FriendlyId
    friendly_id :title, use: :slugged

    scope :featured, -> { where(:featured => true) }

    scope :recent, -> { order(created_at: :desc) }
    scope :hot,    -> { order(view_count: :desc) }
    scope :longest, -> { order(duration: :desc) }


    def self.sort_by(sort_param)
      case sort_param
      when 'recent'
        recent
      when 'hot'
        hot
      when 'longest'
        longest
      else
        recent
      end
    end

end

1 个答案:

答案 0 :(得分:1)

您没有发布Post模型的代码,但假设它具有posted_at属性并且当前显示@post,您可以执行以下操作:

Post.where.not(id: @post.id).order('posted_at desc').limit(6)