I have a scores table with both score and time per user. I want to calculate both averages grouped by user. I can successfully calculate one of them but not sure how to do both at once.
@scores = SpellingScore.where(:user_id => users).average(:score, :group => :user)
Will produce sql like the following
SELECT AVG(`spelling_scores`.`score`) AS average_score, user_id AS user_id
FROM `spelling_scores`
WHERE (`spelling_scores`.`user_id` IN (78767, 78772, 78775)) GROUP BY user_id
I know how to do it in SQL but can't work out the ActiveRecord way.
This is what I want to do...
SELECT AVG(`spelling_scores`.`score`) AS average_score, AVG(`spelling_scores`.`time`) AS average_time, user_id AS user_id
FROM `spelling_scores`
WHERE (`spelling_scores`.`user_id` IN (78767, 78772, 78775)) GROUP BY user_id
Cheers,
Tim