On 21 February 2012 09:39, ruby LED <[email protected]> wrote: > Valery Kvon wrote in post #1047975: >> I didn't see you code completely, but >> >> line_items.to_a.sum { |item| item.total_price } >> >> How can it count correct if only ONE item in the cart? :) >> >> Obviously, if you want to get Array, use Array.wrap method like this: >> >> Array.wrap(line_items).sum { |item| item.total_price } > > i tried Array.wrap(line_items).sum { |item| item.total_price } > also it worked same as the > line_items.to_a.sum { |item| item.total_price } > (i think?) > but im having the issue of not counting my first attemp maybe my > add_product code is wrong? > > def add_product(product_id) > current_item = line_items.where(:product_id => product_id).first > > if current_item > current_item.quantity = current_item.quantity.to_i + 1 > else > current_item = LineItem.new(:product_id=>product_id) > line_items << current_item > end > current_item > end > > it feels like im adding a nil value at my first attemp of adding a > product into my cart?
It is not the item that is nil but total_price. If it were the item that were nil then you would get an error saying that nil did not have a method total_price. Where are you setting total_price when you do LineItem.new. Colin -- 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 [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

