On Aug 31, 12:39 am, Colin Law <clan...@googlemail.com> wrote:
> 2009/8/31 Tony <tony.cassan...@gmail.com>:
>
>
>
> > 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.
>
> Don't follow you here, it should be just one query for each event to
> get the stat summary for that event.
>
> Colin
>
> > 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 <tony.cassan...@gmail.com> 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 <clan...@googlemail.com> wrote:
>
> >> > 2009/8/30 Tony <tony.cassan...@gmail.com>:
>
> >> > > 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

Ooops, sorry about that.  So it is indeed one query per event,
however, I need a list of N events all at the same time.  I didn't
mean to imply that it is N queries per event, rather that it is N
queries per page load.  The page is loading N events and their
summaries (kinda like a dashboard...).  This means that I'd need a
query for every Event to fetch the summary, thus the N queries, + 1
for the initial fetching of the events.  If I were to stuff this in an
association I'd have 2 queries only (one for the events and one for
all of the summaries).  If I were only displaying a few Events per
page I wouldn't be concerned, however, 10, 20, 30+ queries for a
single page would begin to impact performance, I imagine.  I'd really
like to replicate the performance of a find using :include.

If the only way to get that performance is to use a separate model
(SummaryStat) and mark it as readonly, I'm happy to do it.  I was
simply hoping for a more concise and readable way of doing this.  I
got very close to being able to do it in a concise way using the
normal has_* relationships, however, has_one doesn't support grouping
which I needed for this particular query.

Thanks again for bearing with me on this.  I appreciate all your help.

tony
--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to