【问题标题】:Class with nested association to itself与自身嵌套关联的类
【发布时间】:2020-05-20 12:52:34
【问题描述】:

我有一个与自身有嵌套关联的类:

class Location < ActiveRecord::Base
  has_many :location_gateways
  has_many :gateways, through: :location_gateways, class_name: "Location", :dependent => :destroy 
  accepts_nested_attributes_for :gateways, reject_if: :all_blank, allow_destroy: true
  belongs_to :location, optional: true
end

连接模型:

class LocationGateway < ActiveRecord::Base
  belongs_to :location, :class_name => "Location"
  belongs_to :gateway, :class_name => "Location"
end

现在我想制作一个可以创建位置和一些网关的表单,但是当我提交它时收到 Gateway must exist 错误(对 Location.new 的调用)

我假设这是因为模型 Gateway 不存在。我怎样才能让 rails 明白它应该创建另一个 Location 而不是 Gateway

【问题讨论】:

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


    【解决方案1】:

    找不到解决此错误的方法。我必须创建一个继承自 Location 的新类 Gateway,并将 type 添加到 Locations (STI)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 2012-04-25
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      • 2014-10-11
      • 1970-01-01
      相关资源
      最近更新 更多