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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Piercee <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Piercee <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---