Grouping recs by user, then averaging

I’m wondering if there’s a way in RoR other than going to raw SQL to
take a table of scores, group and sum them by each user, then average
those values?

So far, I’ve got:
@gpointsallcum = Gamepoint.average(:pointvalue)|| 0
@gpointsalllastweek = Gamepoint.average(:pointvalue, :conditions
=> [“created_at >= ? and created_at <= ?”,
datelastweek,datethisweek])|| 0
@gpointsallthisweek = Gamepoint.average(:pointvalue, :conditions
=> [“created_at >= ?”, datethisweek]) || 0

But this is just averaging across all scores…

Any help appreciated!

On 24 Jun 2008, at 02:50, Bob W. wrote:

@gpointsallthisweek = Gamepoint.average(:pointvalue, :conditions
=> [“created_at >= ?”, datethisweek]) || 0

sum, average etc… take a :group option if that’s what you’re asking.


On Jun 24, 9:50 am, Bob W. [email protected] wrote:
I think you can use named_scope for last_week and this_week, and
has_many relationships between user and the gamepoint, so that will
give you user.gamepoints.last_week and user.gamepoints.this_week.

Then you can use collect to get the point values and do the sum and