# Summing problem

**URL:** <https://www.ruby-forum.com/t/summing-problem/112915>\
**Category:** Rails\
**Created:** [August 24, 2007, 1:10pm UTC](https://www.ruby-forum.com/t/summing-problem/112915 "2007-08-24T13:10:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![glimjaur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3be4f8/32.png) [@glimjaur](https://www.ruby-forum.com/u/glimjaur)\
**Post date:** [August 24, 2007, 1:10pm UTC](https://www.ruby-forum.com/t/summing-problem/112915/1 "2007-08-24T13:10:38Z")

</div>

I am trying to show the total price of an order, but i keep getting  
this problem;  
wrong number of arguments (1 for 2)

On this method, in the order.rb model;

def total  
order\_items.sum { |item| item.total }  
end

An order consists of several order\_items via a has\_many relationship,  
the total method found in order\_items.rb looks like this;  
def total  
self.quantity \* self.product.price  
end

And as you can guess, an order\_item refers to a product. 🙂

I am simply calling this in the view;  
for orders in @orders do  
order.total  
end

I just cant seem to get it working, help is greatly appreciated. 🙂

---

<div class="post-metadata">

**Author:** ![glimjaur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3be4f8/32.png) [@glimjaur](https://www.ruby-forum.com/u/glimjaur)\
**Post date:** [August 24, 2007, 1:27pm UTC](https://www.ruby-forum.com/t/summing-problem/112915/2 "2007-08-24T13:27:40Z")

</div>

AR::Calculations have a sum() method, so I’d guess it’s not using  
Enumerable#sum.

Try :

order\_items.inject { |total, item| total += item.total }

On 8/24/07, Glimjaur [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> order.total  
> end
> 
> I just cant seem to get it working, help is greatly appreciated. 🙂
> 
> >

–  
Cheers!

- Pratik  
[http://m.onkey.org](http://m.onkey.org)

---

<div class="post-metadata">

**Author:** ![glimjaur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3be4f8/32.png) [@glimjaur](https://www.ruby-forum.com/u/glimjaur)\
**Post date:** [August 30, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/summing-problem/112915/3 "2007-08-30T18:00:50Z")

</div>

On Aug 24, 2007, at 7:26 AM, Pratik wrote:

> > end  
> > I am simply calling this in the view;
> 
> order\_items.inject { |total, item| total += item.total }
> 
> –  
> Cheers!
> 
> - Pratik  
> [http://m.onkey.org](http://m.onkey.org)

Pratik is likely right, but I wanted to correct his use of #inject in  
this case:

order\_items.inject(0.0) { |total, item| total + item.total }

Without a starting point (0.0), the initial value is taken as the  
first element which is an Item not a number. Also, it is unnecessary  
to use += since the local value of total isn’t what’s important, but  
the value of the block (i.e., the last expression value) so just  
using + is sufficient.

As for “why” the order\_items.sum isn’t working, your definition of  
#total is not a database column so the total can’t be obtained from  
the generated SQL directly (at least not without duplicating the  
definition of total and that wouldn’t be very DRY, would it?).

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
