这是我试图理解的代码,目前令人困惑的部分是代码中的 :product_id ,特别是 ":product_id" 的 ":" 部分 我的问题是我们应该如何知道我们应该使用那个 ":" ?
def up
# replace multiple items for a single product in a cart with a single item
Cart.all.each do |cart|
# count the number of each product in the cart
sums = cart.line_items.group(:product_id).sum(:quantity)
sums.each do |product_id, quantity|
if quantity > 1
# remove individual items
cart.line_items.where(product_id: product_id).delete_all
# replace with a single item
item = cart.line_items.build(product_id: product_id)
item.quantity = quantity
item.save!
end
end
end
end