grayson
1
Hello,
I posted this incorrectly on the ruby forum (forgot that it involves
ActiveRecord, so it’s rails).
I wonder how you step down through an association to do a sum. If I
have the following associations
user has_many carts
cart has_many cart_items
cart_item has_one item
I’d like to do a sum of the unit price of all items in the cart and
tried the following
current_user.carts.first.cart_items.items.sum(:unit_price)
current_user.carts.first.cart_items.sum(:item.unit_price)
Neither worked.
TIA
GP
grayson
2
Grayson,
Try adding one more association to your cart –
has_many :items, :through=>:cart_items
With that you can pick your cart (cart = user.carts.first) and
then …
cart.items.sum(:unit_price)
HTH,
AndyV
On Feb 16, 8:50 am, Grayson P. [email protected]
grayson
3
Andy,
That solved the problem. Thanks!
GP
AndyV wrote:
Grayson,
Try adding one more association to your cart –
has_many :items, :through=>:cart_items
With that you can pick your cart (cart = user.carts.first) and
then …
cart.items.sum(:unit_price)
HTH,
AndyV
On Feb 16, 8:50 am, Grayson P. [email protected]