I’m playing around with the Practical Rails Projects code, and was
attempting to make a change for graphing data differently using
groupings.
Basically the code went from:
total_weight = @exercise.activities.collect {|e| e.repetitions
- e.resistance}
workout_dates = @exercise.activities.collect {|e|
e.workout.date.to_s}
To:
total_weight = @exercise.activities.find(
:all,
:select =>
“sum( repetitions * resistance ) AS resistance”,
:group =>
“workout_id”,
:order =>
“workout_id”
).collect{ |e| e.resistance }
workout_dates = @exercise.activities.find(
:all,
:group =>
“workout_id”,
:order =>
“workout_id”
).collect {|e| e.workout.date.to_s}
Now, the total_weight array works fine in the new implementation, but
the generation of it is much less than optimal. I’m renaming the sum
as an existing column so I can collect it, which is just a nasty hack.
How can this be done better? I understand I can use
Enumerable#group_by, but I want the db to do this one.