【问题标题】:Cannot modify association 'SomeModels' because it goes through more than one other association无法修改关联“SomeModels”,因为它通过多个其他关联
【发布时间】: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

【问题讨论】:

    标签: ruby-on-rails rails-admin


    【解决方案1】:

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

    【讨论】:

    • 感谢您的帮助,但这并不是我想要做的。一个商店可能属于一个或多个客户,但它也可能属于一个或多个 ShopGroup。 ShopGroup 也用于给一组店铺命名,模拟一定的层次结构。
    猜你喜欢
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多