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

On Aug 30, 10:04 am, Matt Jones <[email protected]> wrote:
> The "Summary" object you've described doesn't sound like a proper
> ActiveRecord object - is it actually persisted to the database? It
> sounds like you'd be better off just defining a non-AR model (maybe an
> OpenStruct) for the summary. It's not going to help with preloading,
> but you didn't state if that was an issue.
>
> --Matt Jones
>
> On Aug 28, 10:25 pm, Tony <[email protected]> wrote:
>
> > Hey all,
>
> > I've been playing around with special relationships (specifying the
> > select, etc) on my models and trying to get Rails to return the
> > results of database computations as part of an association.  I've got
> > one model representing an object and another model representing
> > statistics about that object on per day basis.  That main Event object
> > has many Stats but it also has one Summary (of those Stats).
> > Unfortunately, Rails won't allow me to have a has one association with
> > computed columns (sums in my case) since it will not allow the use of
> > the group by clause without which Rails can't connect the returned
> > stats to the event since the event_id column is null.  I've gotten
> > around this by using a has_many association for the stats even though
> > I'm grouping on the foreign key and will only ever have one record at
> > most.  This creates some really "hacky-looking" code since I have to
> > read the first element of the returned array everywhere to read the
> > summary stats.  I went digging through the Rails source to take a look
> > at why this might be prevented and I've come up with two thoughts:
>
> > 1) Rails should allow use of :group for a has_one association for
> > specific use cases such as this one and let the programmer decide how
> > it works.  The fix is as simple as adding it to the
> > valid_keys_for_has_one_association in associations.rb.  However, this
> > lead me to a second thought...
>
> > 2) In a has_one association it seems like it would never be
> > detrimental to use a group by clause on the foreign key as this would
> > always return one or fewer results for the parent table.  This relates
> > to the comment at association_preload.rb:148 (in 2.3.3) which states
> > that there is no way to ask the database for one row per distinct
> > foo_id (which is what group by would do, at least in MySQL).
>
> > Am I missing something or is this something that ought to be patched?
> > Thanks for the input and 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 [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
-~----------~----~----~----~------~----~------~--~---

Reply via email to