On 21.02.2012, at 11:15, LED wrote:
> hi guys im playing with RoR with the environment of rails 3 ruby 1.9 i
> got stuck in
>
> nil can't be coerced into BigDecimal
>
> def total_price
> line_items.to_a.sum { |item| item.total_price }
> end
> model/cart.rb
>
That error would be expected when some of line_items.total_price returns nil.
Moreover you can sum total prices without .to_a, that would be more efficient.
> def total_price
> product.price * quantity
> end
>
> def total_price
> if product.price
> product.price * quantity
> else
> product.price = "0.0".to_d
It is not necessary to write attribute :price, just let it return zero.
0.to_d in this case.
> end
> end
Now. before test it again, check in the database if any product has a nil or
any non-bigdecimal value.
--
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.