Dudebot wrote:
> I have a db Orders with fields qty and amount, and another linked db
> Person that has many Orders. I've been using this to sum all of the
> products of qty and amount:
>
> Person.find( person ).orders.inject( 0 ){ |sum, order| sum +
> ( order.qty * order.amount ) }
>
> Is there another/better way to do this in ActiveRecord?
Yes: use the DB's sum() function. ActiveRecord::Calculations could help
you with this if you were summing a single field, but since you're
summing a complex expression, you may have to use raw SQL.
>
> Many TIA,
> Craig
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---