That title wasn’t very descriptive; I wasn’t sure how to shorten my question…
Let’s say I have a table of weightlifters, with name, pounds, and type_of_lift as fields. This table has hundreds of entries in it. I want to retrieve the people with the 10 highest total pounds in the table. I’ve written the following:
Weightlifter.order("pounds DESC").limit(10)
This work fine, except for I want cumulative weights. So if a person is in the top 10 more than once, I don’t want his name to be listed twice, I want to add the weights together and display them as a sum. So if I have:
"Johnny", "300", "Bench"
"Wesley", "295", "Bench"
"Johnny", "280", "Clean"
...
"Timmy", "150", "Curl"
I want to display Johnny with 580 pounds, instead of Johnny with 300 pounds and again later as Johnny with 280 pounds.
How is this done?
Ruby 1.9.3, Rails 3.2.6, SQLite3 3.6.20
Thanks!
It should be something like this
Here’s a good guide: ActiveRecord Query Interface.