# Collection sum

**URL:** <https://www.ruby-forum.com/t/collection-sum/129284>\
**Category:** Ruby\
**Created:** [February 16, 2008, 3:55am UTC](https://www.ruby-forum.com/t/collection-sum/129284 "2008-02-16T03:55:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![grayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@grayson](https://www.ruby-forum.com/u/grayson)\
**Post date:** [February 16, 2008, 3:55am UTC](https://www.ruby-forum.com/t/collection-sum/129284/1 "2008-02-16T03:55:47Z")

</div>

Hello,

I really like the ability to do sums right in the object model, for  
example

@items.sum(:unit\_price), etc! works great!

However I wonder how you step down through an association to do a sum

current\_user.carts.first.cart\_items.items.sum(:unit\_price)

Finding the users first cart works fine but the collection to collection  
between cart\_items to items doesn’t.

TIA

GP

---

<div class="post-metadata">

**Author:** ![grayson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c77e96/32.png) [@grayson](https://www.ruby-forum.com/u/grayson)\
**Post date:** [February 16, 2008, 8:29am UTC](https://www.ruby-forum.com/t/collection-sum/129284/2 "2008-02-16T08:29:24Z")

</div>

This sounds like a Rails question, and you may want to ask it on a  
Rails-specific list; also, its hard to tell what may be going on  
without knowing the definition of the objects being used. With the  
most intuitive layout of objects for what you seem to be doing, I’d  
expect what you are looking for to be something like  
current\_user.carts.first.items.sum(:unit\_price).
