# Sum

**URL:** <https://www.ruby-forum.com/t/sum/71429>\
**Category:** Rails\
**Created:** [October 8, 2006, 6:33pm UTC](https://www.ruby-forum.com/t/sum/71429 "2006-10-08T18:33:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/898d66/32.png) [@eric](https://www.ruby-forum.com/u/eric)\
**Post date:** [October 8, 2006, 6:33pm UTC](https://www.ruby-forum.com/t/sum/71429/1 "2006-10-08T18:33:20Z")

</div>

OK im stuck again… I am following the agile web development with rails  
book and after i made the cart i get an error:

undefined method `sum’ for #Array:0x8ca5e24

the code i have in cart.rb is:  
def total\_price  
@items.sum { |item| item.price }  
end

and in view I have:

Your Cart
 \<% for cart\_item in @cart.items %\> \<% end %\> 

| \<%= cart\_item.quantity %\>× | \<%= h(cart\_item.title) %\> | \<%= number\_to\_currency(cart\_item.price) %\> |
| Total | \<%= number\_to\_currency(@cart.total\_price) %\> |

\<%= button\_to “Empty cart”, :action =\> :empty\_cart %\>

---

<div class="post-metadata">

**Author:** ![eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/898d66/32.png) [@eric](https://www.ruby-forum.com/u/eric)\
**Post date:** [October 8, 2006, 9:17pm UTC](https://www.ruby-forum.com/t/sum/71429/2 "2006-10-08T21:17:33Z")

</div>

Eric wrote:

> OK im stuck again… I am following the agile web development with rails  
> book and after i made the cart i get an error:
> 
> undefined method `sum’ for #Array:0x8ca5e24
> 
> the code i have in cart.rb is:  
> def total\_price  
> @items.sum { |item| item.price }  
> end

the view is not important. the idea is that sum is not a known method  
for an array. by dropping the ‘sum’ method and doing this action  
yourself in the total\_price action like:

def total\_price  
total=0  
@items.each {|item| total += item.price }  
total  
end

you should be getting the same effect.  
you may like to define ‘sum’ in the Array Class, but that may be too  
much for the given task, so i believe the above should be enough.  
hope it helps, somewhat.

harp

---

<div class="post-metadata">

**Author:** ![eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/898d66/32.png) [@eric](https://www.ruby-forum.com/u/eric)\
**Post date:** [October 8, 2006, 9:36pm UTC](https://www.ruby-forum.com/t/sum/71429/3 "2006-10-08T21:36:22Z")

</div>

From the most recent update to the Agile2 PDF:

“I need to stress that the book is written on the assumption that the  
reader  
has Rails 1.2 installed. As that doesn’t yet exist, you HAVE TO HAVE  
EDGE  
RAILS for some of this stuff to work.” - Dave T.

This cart/sum thing is coming up a lot the last couple of days. The  
good  
thing is that instead of just copying lines out of a book, the tutorial  
makes you figure out alternate ways of doing things when they don’t  
work.  
The bad thing is there are a lot of people just starting out, getting  
really  
frustrated.

–f
