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. -Dan Manges http://www.dcmanges.com/blog On Sep 21, 2:05 pm, dschruth <[EMAIL PROTECTED]> 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, Dan Manges <[EMAIL PROTECTED]> wrote: > > > You should take advantage of the association proxies. Something like > > this should work. > > > class Magazine > > has_many :subscriptions > > has_many :subscribers, :through => :subscriptions > > end > > > class Subscription > > belongs_to :magazine > > belongs_to :subscriber > > end > > > class Subscriber > > has_many :subscriptions > > end > > > Now you can do calculations like this: > > > class Magazine > > 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'. > > > -Dan Mangeshttp://www.dcmanges.com/blog > > > On Sep 20, 10:06 pm, dschruth <[EMAIL PROTECTED]> wrote: > > > > Hi, > > > > I'm trying to sum up the records in a related table that used to just > > > be a child table. This line used to work: > > > > table1.sum(:enrich_bead_count_hemo, :conditions => "table2_id = " + > > > id.to_s) > > > > now I've got an intermediate "has and belongs to many" = HABTM table > > > inbetween them. Could somebody help me figure out how to sum across > > > two tables? I'm thinking i might have to use the custom SQL "join" > > > attribute of the sum method. > > > > Thanks, > > > > Dave --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

