An odd titled name for something that I had to remember how to do today. If you need to sort something by the number of its has_many object, eg…

Album has_many photos, listing albums with the most photos first
Forum has_many posts, listing forums with the most posts first
Group has_many users, listing groups with the most users first

…and you want to show these parents ordered by the number of children objects they have you need to do this…

a = Album.find(:all).sort { |x,y| x.photos.size <=> y.photos.size }

Obviously you can use named_scopes and conditions to restrict it down if you don’t want all of a certain object, either child or parent.

Hopefully this helps someone out.