【发布时间】:2014-10-14 08:04:20
【问题描述】:
尝试创建/编辑具有一些“has_many ... through”元素的实例时出现错误。我一直在寻找解决方法,我尝试在一些和所有关系上添加“,:autosave => false”,但似乎没有任何效果。
ActiveRecord::HasManyThroughNestedAssociationsAreReadonly at /customer/new
Cannot modify association 'Customer#shops' because it goes through more than one other
association.
这是我的Customer 模型,这是我想要创建/编辑的模型:
class Customer < ActiveRecord::Base
belongs_to :user
has_many :customer_shop_groups
has_many :shop_groups, -> { order(:name) }, through: :customer_shop_groups
has_many :shops, -> { order(:name) }, through: :shop_groups
has_one :api_consumer
end
这些模型也具有“has_many...through”依赖项:
这是我的CustomerShopGroup 模型:
class CustomerShopGroup < ActiveRecord::Base
belongs_to :customer
belongs_to :shop_group
has_many :shops, through: :shop_group
end
这是我的ShopGroup 模型:
class ShopGroup < ActiveRecord::Base
has_many :shops
has_many :customer_shop_groups
has_many :customers, through: :customer_shop_groups
has_many :shop_group_maps, -> { order 'level' }, class_name: 'Map'
has_many :heatmaps, through: :shop_group_maps
scope :with_friendly_id, lambda { |friendly_id| where(friendly_id: friendly_id) }
end
这是我的ApiConsumer 模型:
class ApiConsumer < ActiveRecord::Base
belongs_to :customer
has_many :shops, through: :customer
end
【问题讨论】: