【发布时间】: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