I think I understand what you're trying to do - you don't need to do a
find. Implement the method like I suggested.
has_many :subscribers, :through => :subscriptions
def some_method
subscribers.calculate(:max, :date_of_birth)
end
"subscribers" references the has_many :through association proxy. It
will take care of setting the proper joins and conditions to run the
calculation only for the associated records. In my example with
calculate(:max, :date_of_birth), the method would find the date of
birth for the youngest person subscribed to that magazine. Make sure
you're using lowercase "subscribers", not capitalized "Subscriber"
-Dan Manges
http://www.dcmanges.com/blog
On Sep 22, 1:41 pm, dschruth <
dschr...@gmail.com> wrote:
> Lets just use yourMagazine/Subscriberexample for now.
>
> I want to do something like this:
> --------------------------------------------------------
> classMagazine
> def subscriber_salaries
> Subscriber.calculate(:sum, :salary, :conditions => "magazine_id =
> " + id.to_s) # PGError: ERROR: column "magazine_id" does not
> exist
> end
> end
> --------------------------------------------------------
> But of course there is no "magazine_id" in the subscribers table
> anymore because we've switched away from "subscriberhas many
> subscriptions".
>
> Like you said, I'm guessing I should probably use a "find" like so:
>
> some_model =Subscriber.find( ??? )
> some_model.Subscriber.calculate(:sum, :salary)
>
> But i don't know how to do that quite yet. (what goes in the ???)
>
> Ideas?
>
> On Sep 21, 12:40 pm, DanManges<
daniel.man...@gmail.com> wrote:
>
> > Hi Dave,
>
> > Can you post more of your code? If you're going through the
> > association proxies, the calculation should work. Is 'table1' in your
> > example a target of a has_many :through association? So do you have it
> > set up something like this?
>
> > class SomeModel < ActiveRecord::Base
> > has_many :table1, :through => :something
> > end
>
> > some_model = SomeModel.find(:first)
> > some_model.table1.sum(:enrich_bead_count_hemo)
>
> > If you're just doing Table1.sum(:enrich_bead_count_hemo), (note the
> > capitalized Table1), then it would be for all the rows, not just the
> > associated ones.
>
> > -DanMangeshttp://
www.dcmanges.com/blog
>
> > On Sep 21, 2:05 pm, dschruth <
dschr...@gmail.com> wrote:
>
> > > Thanks for the response,
>
> > > However, I tired what you suggested but only got to the same point as
> > > before.
>
> > > Both of these ways:
>
> > > table1.calculate(:sum, :enrich_bead_count_hemo,
> > > OR
> > > table1.sum(:enrich_bead_count_hemo
>
> > > give me the same answer: at total of *all* the rows in the other
> > > table. But I want a sum of *just* the *associated* rows.
>
> > > I was able to do this when the tables where directly related (with
> > > a :condition statement). But I don't quite know how to do that yet
> > > for HABTM or "has many :foo => :through"
>
> > > Dave
>
> > > On Sep 20, 9:53 pm, DanManges<
daniel.man...@gmail.com> wrote:
>
> > > > You should take advantage of the association proxies. Something like
> > > > this should work.
>
> > > > classMagazine
> > > > has_many :subscriptions
> > > > has_many :subscribers, :through => :subscriptions
> > > > end
>
> > > > class Subscription
> > > > belongs_to :magazine
> > > > belongs_to :subscriber
> > > > end
>
> > > > classSubscriber
> > > > has_many :subscriptions
> > > > end
>
> > > > Now you can do calculations like this:
>
> > > > classMagazine
> > > > def youngest_subscriber_birthday
> > > > subscribers.calculate(:max, :date_of_birth)
> > > > end
> > > > end
>
> > > > You should be able to do the calculation the same way for your
> > > > 'enrich_bead_count_hemo'.
>
> > > > -DanMangeshttp://
www.dcmanges.com/blog