You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
- class Cart < ApplicationRecord
- has_many :line_items, dependent: :destroy
-
- def add_product(product)
- current_item = line_items.find_by(product_id: product.id)
- if current_item
- current_item.quantity += 1
- else
- current_item = line_items.build(product_id: product.id)
- end
- # QUESTION: What happens if the price change in between to additions?
- current_item.price = product.price
- current_item
- end
-
- def total_price
- # QUESTION: How and why? :-D
- line_items.to_a.sum { |item| item.total_price }
- end
- end
|