Skip to content

Instantly share code, notes, and snippets.

@autumn-hoerr
Created October 3, 2013 00:45
Show Gist options
  • Save autumn-hoerr/6802826 to your computer and use it in GitHub Desktop.
Save autumn-hoerr/6802826 to your computer and use it in GitHub Desktop.
order a model by the sum of an associated model in rails using pg
# http://stackoverflow.com/questions/10526012/order-players-on-the-sum-of-their-association-model
scope :order_by_distance, joins(:activities).select('users.id, users.name, sum(activities.miles) as total_distance').group('users.id').order('total_distance desc')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment