Forum: Ruby on Rails Order By Number of Comments

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B28d0b9ff630f071d8867aa164f27a80?d=identicon&s=25 Hamza (Guest)
on 2006-04-03 16:36
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
This topic is locked and can not be replied to.