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)
精彩评论