开发者

sort by childs count

I want to order by Posts comments count. But this code is not working. Any help will be appreciated.

model Post

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

model Comment

belongs_to :post


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜