Thanks Dan that worked! I suppose at some point here I should learn ruby. :) The language (and rails platform) give you so much for free and the syntax is so intuitive that Its easy to get way in over your head fast when the subtle but inevitable complexities start to counteract the initial advantages! (perhaps)
At the very least I should figure out the difference between these three object naming conventions Subscriber # CamelNotation (the class?) subscriber # lowercase_underscore (an instantiation of the class? ie an object?) :subscriber # :colon_lowercase_underscore (an accessor? what ever that is.) (not to mention the plural vs singular conventions) Its very confusing for newbies Thanks though, this one trick should keep me from stumbling over my own ignorance for a while. Dave On Sep 22, 1:12 pm, Dan Manges <[EMAIL PROTECTED]> wrote: > 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 Mangeshttp://www.dcmanges.com/blog > > On Sep 22, 1:41 pm, dschruth <[EMAIL PROTECTED]> 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<[EMAIL PROTECTED]> 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 <[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, DanManges<[EMAIL PROTECTED]> 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 > > > > > > 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 -~----------~----~----~----~------~----~------~--~---

