This code is assigning a 0 to fixed price of a product
(most probably not what is intended). Probably simply
return a 0 (in BigDecimal) here.
end
end
but still this wont work
Is there a business rule that each item must realistically have
a quantity and product (and that product must have a price) ?
In that case I suggest:
add validations for the presence of these values
(presence of quantity and product on item
presence of price on product)
write code like:
def total_price
if self.valid?
product.price * quantity
else
0.to_d
end
end
Another possible gotcha is that “item.product_id” is available
(e.g. from a select box), but item.product is not yet associated
(you may need to override the product_id= setter for that e.g.
def product_id=(product_id)
self.product = Product.find_by_id(product_id)
super
end
Is there a business rule that each item must realistically have
a quantity and product (and that product must have a price) ?
uhm im following agile web development rails actualy the snippets are
from the book
add validations for the presence of these values
(presence of quantity and product on item
presence of price on product)
i already validated the price
of each item in CRUD
yes those data are valid actualy a while ago it worked but when i
clear and destroy the cart it returned to this error
very much appreciated for your effort thank you
For clarity. It is not because a validation is present that the
value is always present in memory (in the database, using the
standard techniques for saving, only valid data should ever be
really saved).
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.
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.
tried to applying this but gotting the problem in the view
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.
i tried this and it worked out but when reseting the cart it return the
error
end
end
Now. before test it again, check in the database if any product has a
nil or any non-bigdecimal value.
this is being weird when i tried your suggestion it worked out but when
i
clear delete the cart again it return to the error
i tried refactoring my destroy method now its working fine but my second
issue is when i add item to the cart at first attemp it returns to that
error again but returning to the product list it worked fine im just
wondering if my add_product is code is ok?
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 of adding
product in to the cart, but return to home and clicking it second time
it work but didnt count my first attemp of adding a product maybe my
add_product code is wrong?
Ok, let’s imagine. You add new LineItem and product.price is nil or
quantity is nil
then
product.price * quantity
would raise error “nil can’t be coerced into”
Check if product price or quantity is nil after initialize
LineItem.new
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.
i belive im setting it in the current_item it self
current_item = LineItem.new(:product_id=>product_id)
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.
current_item = LineItem.new(:product_id=>product_id) = BAD IDEA.
Because, if you pass the invalid product ID, LineItem would be
initialized as well, but the .product association would return nil.
Actually I didn’t see how quantity is set up? Looking at your
previous code it must be method ‘quantity’.
current_item = LineItem.new(:product_id=>product_id) = BAD IDEA.
Because, if you pass the invalid product ID, LineItem would be
initialized as well, but the .product association would return nil.
do you think current_item = line_items.build(:product_id => product_id)
is better?
Actually I didn’t see how quantity is set up? Looking at your
previous code it must be method ‘quantity’.
i dont have a method quantity
PS. Why are you writing product.price=?
i belive it is same with product.price * quantity
to show the sum of all products inside my cart
PS. Why are you writing product.price=?
i belive it is same with product.price * quantity
to show the sum of all products inside my cart
Logic, logic, logic.
product - is a stand alone instance or One product. Why Would it ever
have the multiplied price with ‘quantity’ ?.
I remember many years ago I read the beginner’s book “Agile Web
Development with Rails”, is that example from it? I think you missed
something.
thank very much for the effort yes this is from the book agile web
development with rails sorry about it im just starting to learn ruby on
rails