gpt4 book ai didi

ruby-on-rails - 无法修改关联 'SomeModels',因为它经过多个其他关联

转载 作者:行者123 更新时间:2023-12-04 07:32:08 24 4
gpt4 key购买 nike

我在尝试创建/编辑具有一些“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

最佳答案

您似乎正试图将商店保存在客户表单中,但它不起作用。一种方法是替换 has_many :shops, -> { order(:name) },通过: :shop_groups 创建一个 has_and_belongs_to_many 关联。

关于ruby-on-rails - 无法修改关联 'SomeModels',因为它经过多个其他关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26355659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com