我有一个与自身嵌套关联的类:
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
?
找不到解决此错误的方法。我必须创建一个继承自Gateway
的新类Location
,并将type
添加到位置(STI)。