My Ruby on Rails method is as follows:

def top_posts
    Post.order('count(:comments)').first(3)
end

What I am trying to do is return the first three posts ordered by the number of comments on each post.

有帮助吗?

解决方案

Try this:

   Post.first(3).sort! { |a,b| b.comments.count <=> a.comments.count}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top