Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. class Cart < ActiveRecord::Base
  2. has_many :cart_items, :dependent => :destroy
  3. has_many :items, :through => :cart_items, :source => :product
  4. def add_product_to_cart(product)
  5. items << product
  6. end
  7. def update_quantity_to_cart(product_id, quantity, current_cart_id)
  8. ## do something..
  9. end
  10.  
  11. # diff
  12. #def total_price(cart_id)
  13. def total_price #不需要添加(cart_id)
  14.  
  15. #找出這台車有的產品及產品價格(test)
  16. #items.each(){ |item| item.price}
  17. #找出數量(test)
  18. #artItem.where(product_id: 2,cart_id: 5).take!.quantity
  19. #運算
  20.  
  21. #diff
  22. #items.inject(0){|sum, item| sum + item.price * CartItem.where("product_id = ? AND cart_id = ?", item.id, cart_id).take!.quantity }
  23. items.inject(0) { |sum, item| sum + (item.price * self.cart_items.find_by(:product_id => item.id).quantity) } # 因為此為 current_cart 的方法,所以不用再利用 cart_id 了,這樣會更方便
  24. #note利用.map & 把quantity拆成另一個method
  25. end
  26. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement