Maybe I'm missing something but I don't see any way to get eager loading with the named scopes. I have many Event models each with many Stat models. If I have to query each event to get the summary, I still have the n+1 query problem. Maybe the best solution is to define a second model referencing the same table but with a different default scope? That doesn't seem like it is the most concise solution but it may be the cleanest and easiest to read.
I think the ideal solution would be the ability to reference a named scope of a joined model. E.g: Event: has_one :summary, :class_name => 'Stat', :scope => :summary Does that make sense? Is there some other way that I'm overlooking? Thanks again! tony On Aug 30, 2:36 pm, Tony <[email protected]> wrote: > Thanks for the quick response! I'm not familiar with the named > scopes. Will these work with preloading the records (I assume by > using (:include => [:stats])? I could end up with a ton of Events on > one page and I'd prefer not to have to fetch the summary for every > one. I also assume I'd reference it by doing event.stats.summary? > That seems a lot cleaner. I'll go dig into this a bit. Thanks! > > tony > > On Aug 30, 2:24 pm, Colin Law <[email protected]> wrote: > > > 2009/8/30 Tony <[email protected]>: > > > > So I'd like to set up the below associations. An Event is a normal AR > > > model. Stat is a normal AR model. I want a special stat on Event > > > that is a DB computation (mostly just sums of each column) and a > > > summary of all the Stats for that Event. The Summary isn't persisted > > > and is marked as readonly. Unfortunately, preloading is important in > > > this case (and thus why I went off on the group_by tangent, sorry if > > > that wasn't clear). > > > > I guess I could make a new model for Summary referencing the stats > > > table which uses a special select and always adds the group by clause, > > > however, that seemed like overkill. I really didn't want to run a > > > loop over the returned stats since the DB can give me exactly what I'm > > > looking for on the initial query. > > > > Ok, here's a slightly abridged version of what I thought might work: > > > > Event: > > > has_many :stats > > > has_one :summary_stat, :select => "sum(people) as > > > people, ...", :class_name => 'Stat', :foreign_key => > > > 'event_id', :readonly => true > > > > Stat: > > > belongs_to :event > > > > Adding "group" to the has_one solves the issue, but am I going about > > > this in the "right" way? Thanks for the input! > > > > tony > > > I don't think I would go about it this way. Using Event has_one > > summary_stat suggests that somewhere there is a model SummaryStat that > > belongs_to event. > > An alternative would be to define an appropriate named scope in Stat > > that provides the summed stats using the single query and provide a > > read only attribute of Event that calls it and provides the answer. > > > Colin --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

