if product.price
product.price * quantity
else
product.price = "0.0".to_d
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
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
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
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
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
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 maybe my
add_product code is wrong?
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.
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)
I agree that:
1) 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.
2) Actually I didn't see how quantity is set up? Looking at your previous code it must be method 'quantity'.
I agree that:
1) 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?
2) 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
I agree that:
1) 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?
no, the correct way is:
product = Product.find(product_id) # it will raise AcriveRecord::RecordNotFound if invalid product_id passed.
current_item = LineItem.new(:product => product) # or current_item = LineItem.new; current_item.product = product
2) 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
In this case it does not work ever. def total_count - is an instance method.
If you use 'quantity', so it just MUST be an another instance method or local variable. There is something wrong with app's logic.
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.