문제

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