Order By Number of Comments


#1

Hi,

I am new to Ruby on Rails, so I apologise if this has been answered
elsewhere. I have had a look through the list but I can’t seem to find
what I am trying to do.

I have implemented the standard blog and comments app from the
screencast. However I am trying to extend the functionality.

I want to be able to display a list of posts and order them by the total
number of comments they have. It will act as a most discussed view.

This is what I have been trying in the controller, with no luck :

def show_discussed
@contents.find(:all, :order => “contents.comment.size DESC”, :joins =>
“comments”)
end

I have got some SQL that works : (I renamed posts to contents)

select contents., count() as comment_total from contents, comments
WHERE contents.id = comments.content_id group by contents.id;

But this does not return the posts with no comments.

I would really appreciate any help.

Thank you in advance.

Hamza