我在尝试创建/编辑具有一些“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
答案 0 :(得分:0)
您似乎正在尝试在“客户”表单中保存商店,但它不起作用。您可以采用的一种方法是替换has_many :shops, -> { order(:name) }, through: :shop_groups
创建has_and_belongs_to_many
关联。