Thanks dan, I'll use this one for now..  We're still a pretty small,
internal, few-records shop.

On Sep 28, 4:42 pm, Dan Manges <[EMAIL PROTECTED]> wrote:
> Hi Dave,
>
> You can use methods from the Enumerable module to get a list of
> salaries and calculate the sum. Try something like this:
>
>   def subscriber_salaries
>     subscribers.map(&:salary).sum
>   end
>
> That will loop over each subscriber, build an array of the salaries,
> and then calculate the sum. I would use this approach unless due to
> the size of the data set you want to bring back the result directly
> from the database.
>
> Hope this helps,
>
> Danhttp://www.dcmanges.com/blog
>
> On Sep 28, 4:14 pm, dschruth <[EMAIL PROTECTED]> wrote:
>
> > Hey Dan,
>
> > Got another question about this.
>
> > What if salary is not really a column in the database?
>
> > What if it's a variable which is just a function of two other database
> > columns?
>
> > For example what if 'salary' is a variable defined in the Magazine
> > object but it's either 'gross_income' or 'gross_pay'  (depending on
> > which is selected via a third column's value) ?
>
> > When I try to use the non-column *variable* salary below, i get the
> > following error:
>
> > "PGError: ERROR:  column "salary" does not exist"
>
> > Basically I'm just asking how to sum up related objects' variables.
> > (which are not stored in the database)
>
> > On Sep 22, 1:14 pm, Dan Manges <[EMAIL PROTECTED]> wrote:
>
> > > On Sep 22, 1:41 pm, dschruth <[EMAIL PROTECTED]> wrote:
>
> > > > Lets just use your Magazine / Subscriber example for now.
>
> > > > I want to do something like this:
> > > > --------------------------------------------------------
> > > > class Magazine
> > > >   def subscriber_salaries
> > > >      Subscriber.calculate(:sum, :salary, :conditions => "magazine_id =
> > > > " + id.to_s)     # PGError: ERROR:  column "magazine_id" does not
> > > > exist
> > > >   end
> > > > end
>
> > > You need to implement subscriber_salaries like this:
>
> > > class Magazine
> > >   has_many :subscriptions
> > >   has_many :subscribers, :through => :subscriptions
> > >   def subscriber_salaries
> > >     subscribers.calculate(:sum, :salary)
> > >   end
> > > end
>
> > > -Dan
--~--~---------~--~----~------------~-------~--~----~
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