Class 与自身有嵌套关联
Class with nested association to itself
我有一个 class 与自身的嵌套关联:
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
?
找不到解决此错误的方法。我必须制作一个新的 class Gateway
继承自 Location
并向 Locations (STI) 添加 type
。
我有一个 class 与自身的嵌套关联:
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
?
找不到解决此错误的方法。我必须制作一个新的 class Gateway
继承自 Location
并向 Locations (STI) 添加 type
。