2011-03-21 31 views
1

我要訂購的文章評論數。但是這個代碼不起作用。任何幫助將不勝感激。按小孩計數

模型後

has_many :comments 
scope :active, where("active = ?", true) 
scope :popular, active.limit(10).sort { |a, b| b.comments.size <=> a.comments.size } 

模型評論

belongs_to :post 

回答

2
has_many :comments 
scope :active, where("active = ?", true) 
scope :popular, active.joins(:comments).select("posts.*, count(posts.id) as a_c").group("comments.post_id").order("a_c DESC").limit(10) 
+0

感謝,但它也不是沒有意見 – hakaooa 2011-03-21 09:40:04

+0

什麼上市的職位?我實際上並不瞭解你需要什麼 – fl00r 2011-03-21 09:53:59

+0

它按他們的評論大小列出帖子,但如果帖子有0條評論,它不在列表中 – hakaooa 2011-03-21 10:05:36