【问题标题】:ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflectionActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection
【发布时间】:2015-07-13 11:10:47
【问题描述】:

我有以下型号:

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'

关系有什么问题?

【问题讨论】:

  • 如果有效,请接受我的回答:)

标签: ruby-on-rails nested-attributes rails-models


【解决方案1】:

您的关联设置有误。应该是

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 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 2017-04-14
    • 2015-06-23
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    相关资源
    最近更新 更多