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.

¿Fue útil?

Solución

Try this:

   Post.first(3).sort! { |a,b| b.comments.count <=> a.comments.count}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top