這些都是我的模型如何設置連接表的額外屬性?
class Product
has_many :line_items
has_many :orders, :through => :line_items
end
class LineItem
belongs_to :order
belongs_to :product
validates :quantity, presence: true
end
class Order
has_many :line_items
has_many :products, :through => :line_items
end
我可以做@order.products << @product
對產品與訂單相關聯,但我無法弄清楚如何還建立在連接表中的數量。
我不得不這樣做'line_items.build',而不是'line_items.create 「讓它起作用,否則這是最簡單的解決方案。 – lala