Why do I get an Unknown Primary Key exception for a join table in Rails 4?

后端 未结 5 515
情歌与酒
情歌与酒 2021-01-04 09:21

These are my models:

class Product
  has_many :line_items
  has_many :orders, :through => :line_items
end

class LineItem 
  belongs_to :order
  belongs_t         


        
5条回答
  •  伪装坚强ぢ
    2021-01-04 09:45

    In model add the following:

    self.primary_key = [:order_id, :product_id]
    

    and I think it would be wise to ensure that there's an index on those columns.

    You may create one with following migration:

    add_index :line_items, [:order_id, :product_id]
    

提交回复
热议问题