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.
How can this be done better? I understand I can use
Enumerable#group_by, but I want the db to do this one.
Rob,
If you want the db to do this one then just do the optimal SQL query
yourself and load it up, for one, it will probably be easier to read
than the complex meta you currently have.
I am never afraid to get my hands dirty with SQL as I am already
committed to a particular vendor and sometimes I can fine tune the query
much better than rails.
How can this be done better? I understand I can use
Enumerable#group_by, but I want the db to do this one.
Rob,
If you want the db to do this one then just do the optimal SQL query
yourself and load it up, for one, it will probably be easier to read
than the complex meta you currently have.
exercises.id )
WHERE exercises.id = ?
AND exercises.user_id = ?
GROUP BY workout_id
ORDER BY workout_id
", @exercise.id, @exercise.user_id] ).collect{ |e|
e.total_weight }
Another little trick if you are using MySQL is to use the undocumented
method all_hashes in the mysql adapter which converts the unwieldy
result set into a regular hash which is a lot more convenient to work
with…
Nah, I’m a SQLite and PostgreSQL guy… Which meant my sql was bad
anyway. So now it’s:
SELECT sum( repetitions * resistance ) AS total_weight,
MIN( date ) AS date
FROM activities
JOIN workouts ON( activities.workout_id = workouts.id )
JOIN exercises ON( activities.exercise_id = exercises.id )
WHERE exercises.id = ?
AND exercises.user_id = ?
GROUP BY workout_id
ORDER BY workout_id
exercises.id )
WHERE exercises.id = ?
AND exercises.user_id = ?
GROUP BY workout_id
ORDER BY workout_id
", @exercise.id, @exercise.user_id] ).collect{ |e|
e.total_weight }
Cheers,
Rob
Another little trick if you are using MySQL is to use the undocumented
method all_hashes in the mysql adapter which converts the unwieldy
result set into a regular hash which is a lot more convenient to work
with…
ilan
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.