这里是我想了解的一个代码,现在令人困惑的部分是:代码中的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
在这段代码中,你给它并不重要。一般来看看方法文档,看它是否期待符号或字符串(或它们之间的区别) –