0
我正在通過敏捷Web開發與Rails教程。有產品,LineItems和購物車。敏捷Web開發line_item創建
產品
class Product < ActiveRecord::Base
attr_accessible :description, :image_url, :price, :title
has_many :line_items
end
的LineItem
class LineItem < ActiveRecord::Base
attr_accessible :cart_id, :product_id, :product
belongs_to :product
belongs_to :cart
end
車
class Cart < ActiveRecord::Base
has_many :line_items, dependent: :destroy
end
LineItemsController
class LineItemsController < ApplicationController
def create
@cart = current_cart
product = Product.find(params[:product_id])
@line_item = @cart.line_items.build(product: product)
....
end
我的問題是關於在上面的創建操作3號線。我明白將一個product_id傳遞給line_items.build(),但我不明白傳遞整個產品的作用是什麼?
感謝, 麥克