Cameron Caine wrote in post #965772: > Hello > > I have few typical models: > > class Team < AR::Base > has_many :games > end > > class Game < AR::Base > belongs_to :team > has_many :events > end > > etc.. > > When I list the team.games I want to be able to aggregate some of the > data (games won, lost etc). I could do this using scopes, but there are > so many different calculations it would result in a lot of queries. > > As I am already fetching all the team games in one DB call, should I be > doing the calculations using just plain ruby on the fly, or even on the > client side using JSON/JS? Or is it better in fact to push everything > through SQL.
Use SQL as it was meant to be used! The DB can do the aggregate calculations faster than Rails could. You can even have the aggregates returned in the same query with everything else (though I'm not sure I'd advise that). ActiveRecord::Calculations provides a nice interface to SQL aggregate functions (at least in Rails 2 -- Arel may take care of this in Rails 3). > > I'm not sure of the best plan of attack to do these calculations given > that they could well involve pulling data from the children of each game > as well (e.g events). What are the calculations that you currently need? > > Many thanks in advance for any insight. I am pulling my hair out here! > > Cameron Best, -- Marnen Laibow-Koser http://www.marnen.org [email protected] -- Posted via http://www.ruby-forum.com/. -- 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 [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

