2015-07-13 47 views
1

我有以下型號:的ActiveRecord :: HasManyThroughCantAssociateThroughHasOneOrManyReflection

class Order < ActiveRecord::Base 
    has_many :order_products, foreign_key: 'order_foreign_id' 
    has_many :order_variation_values, through: :order_products 
    accepts_nested_attributes_for :order_variation_values 
end 

class OrderProduct < ActiveRecord::Base 
    has_many :order_variation_value 
end 

class OrderVariationValue < ActiveRecord::Base 
    belongs_to :order_product, foreign_key: 'order_product_foreign_id' 
end 

當我嘗試添加記錄與nested_attributes我得到這個錯誤:

ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection (Cannot modify association 'Order#order_variation_values' because the source reflection class 'OrderVariationValue' is associated to 'OrderProduct' via :has_many.): app/controllers/api/v2/orders_controller.rb:8:in 'create'

什麼是錯的關係?

+0

請接受我的答案,如果工作:) – Pavan

回答

2

您的關聯設置是錯誤的。它應該是

class Order < ActiveRecord::Base 
    has_many :order_products, foreign_key: 'order_foreign_id' 
    has_many :order_variation_values, through: :order_products 
    accepts_nested_attributes_for :order_variation_values 
end 

class OrderProduct < ActiveRecord::Base 
    belongs_to :order 
    belongs_to :order_variation_value 
end 

class OrderVariationValue < ActiveRecord::Base 
    has_many :order_products, foreign_key: 'order_foreign_id' 
    has_many :orders, through: :order_products 
end 

查看這些Guides瞭解更多信息。