# Sum product of 2 fields?

**URL:** <https://www.ruby-forum.com/t/sum-product-of-2-fields/176283>\
**Category:** Rails\
**Created:** [September 28, 2009, 3:13pm UTC](https://www.ruby-forum.com/t/sum-product-of-2-fields/176283 "2009-09-28T15:13:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 28, 2009, 3:13pm UTC](https://www.ruby-forum.com/t/sum-product-of-2-fields/176283/1 "2009-09-28T15:13:11Z")

</div>

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?

Many TIA,  
Craig

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 28, 2009, 3:23pm UTC](https://www.ruby-forum.com/t/sum-product-of-2-fields/176283/2 "2009-09-28T15:23:59Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 28, 2009, 4:52pm UTC](https://www.ruby-forum.com/t/sum-product-of-2-fields/176283/3 "2009-09-28T16:52:07Z")

</div>

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-

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 28, 2009, 5:05pm UTC](https://www.ruby-forum.com/t/sum-product-of-2-fields/176283/4 "2009-09-28T17:05:53Z")

</div>

Dudebot wrote:

> 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.

You got it backwards. In this case, doing the calculation on the DB  
side will be more maintainable.

> The db is  
> small, and performance degradation should not be an issue.

I wonder. What you’re doing now sends lots of unnecessary data from the  
DB to the app. Doing it on the DB side will avoid this problem.

I don’t see a single good reason for doing the calculation in Ruby in  
this case.

> On Sep 28, 8:23ï¿½am, Marnen Laibow-Koser \<rails-mailing-l…@andreas-

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
