RE: Order By Number of Comments


#1

One other thing you could do is add an auto-populating field to the
parent in the relationship. So, say I have a post model, which has_many
:comments. Using a magic field of comments_count, this will be
auto-updated when comments are added or removed from the post. So, I
could get the list in the order you want, by simply doing…

.find(:all, :order_by => ‘comments_count DESC’, :conditions =>
[‘comments_count > x’])

And then I can forget about joins etc… Handy magic field name! (Saves
doing a count - note that doing .count on comments still uses a count
statement).

Hope that’s useful…

Andrew