25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

31 lines
827 B

  1. class CombineItemsInCart < ActiveRecord::Migration[5.0]
  2. def up
  3. Cart.all.each do |cart|
  4. sums = cart.line_items.group(:product_id).sum(:quantity)
  5. sums.each do |product_id, quantity|
  6. if quantity > 1
  7. cart.line_items.where(product_id: product_id).delete_all
  8. item = cart.line_items.build(product_id: product_id)
  9. item.quantity = quantity
  10. item.save!
  11. end
  12. end
  13. end
  14. end
  15. def down
  16. LineItem.where('quantity > 1').each do |li|
  17. li.quantity.times do
  18. # item = Cart.find_by(:cart_id li.cart_id).line_items.build(product_id: li.product_id)
  19. # item.save!
  20. LineItem.create(cart_id: li.cart_id,
  21. product_id: li.product_id,
  22. quantity: 1)
  23. end
  24. li.destroy
  25. end
  26. end
  27. end