1

I'd like to order a model query result by the sum of multiple has_many associations. For example, a User query method that orders results based on his/her total count of (comments + answers + votes). These are all models that belong to User.

I use a User instance method, children, to lump together these associations already, but I'm having trouble getting ActiveRecord's order method to order by children.count.

I want this to work (but it doesn't):

User.all.order("#{children.count} DESC")

How would one do this?

eeeeeean
  • 1,774
  • 1
  • 17
  • 31
  • 1
    Duplicate of: http://stackoverflow.com/questions/16996618/rails-order-by-results-count-of-has-many-association (The second-highest-rated answer is the correct solution for most.) – Jordan Running Jan 22 '16 at 23:59

0 Answers0