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.
 
 
 
 
 

19 regels
434 B

  1. class Cart < ApplicationRecord
  2. has_many :line_items, dependent: :destroy
  3. def add_product(product)
  4. current_item = line_items.find_by(product_id: product.id)
  5. if current_item
  6. current_item.quantity += 1
  7. else
  8. current_item = line_items.build(product_id: product.id)
  9. end
  10. current_item
  11. end
  12. def total_price
  13. # QUESTION: How and why? :-D
  14. line_items.to_a.sum { |item| item.total_price }
  15. end
  16. end