1) There are a couple of ways to do this, easiest would be counter cache, you do that my creating a column to maintain the count and rails will keep the count up to speed. the column in this case would be comments_count
songs = Song.all(:order => "comments_count DESC")
OR you could do a swanky query:
songs = Song.all(:joins => "LEFT JOIN comments ON songs.id = comments.song_id",
:select => "song.name, count(*)",
:group => "song.name",
:order => "count(*) DESC")
a few caveats with the second method, anything you want to select in the songs you will need to include in the group by statement. If you only need to pull songs with comments then you can:
songs = Song.all(:joins => :comments,
:select => "song.name, count(*)",
:group => "song.name",
:order => "count(*) DESC")
Which looks nicer but because it does an inner join you would not get songs that had no comments
2) just an include/joins
songs = Song.all(:include => :comments, :order => "comment.created_at"
I hope this helps!