Thanks--I tried a bunch of different incarnations using sum() without
luck, and as I was starting to write raw SQL, I decided it would be
easier to read/more maintainable code just to use Ruby.  The db is
small, and performance degradation should not be an issue.

On Sep 28, 8:23 am, Marnen Laibow-Koser <rails-mailing-l...@andreas-
s.net> wrote:
> 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-Koserhttp://www.marnen.org
> [email protected]
> --
> Posted viahttp://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
-~----------~----~----~----~------~----~------~--~---

Reply via email to