Created
October 3, 2013 00:45
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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