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! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 24 Jun 2008, at 02:50, Bob Walsh wrote:> > 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 >sum, average etc... take a :group option if that''s what you''re asking. Fred> But this is just averaging across all scores... > > Any help appreciated! > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jun 24, 9:50 am, Bob Walsh <bob.wa...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 average.> 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!--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---