Try this:
Post.first(3).sort! { |a,b| b.comments.count <=> a.comments.count}
Pregunta
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.
Solución
Try this:
Post.first(3).sort! { |a,b| b.comments.count <=> a.comments.count}