具有自身嵌套关联的类

问题描述 投票:1回答:1

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

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个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.