RE: Order By Number of Comments

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

Hope that’s useful…


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs