On 21.02.2012, at 15:29, ruby LED wrote: > Valery Kvon wrote in post #1047990: >> >> >> 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. -- 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.

