【发布时间】:2023-03-09 00:02:01
【问题描述】:
我有以下型号:
Client.rb
has_many :establishments
accepts_nested_attributes_for :establishments
has_many :addressess, through: :establishments
accepts_nested_attributes_for :addresses
Establishment.rb
belongs_to :address
belongs_to :client
Address.rb
has_many :establishments
在Client 的show 视图中,我创建了一个<%= link_to "New Establishment", new_client_address_path(@client)%>,以便为客户创建一个新的地址记录和一个新的机构。
在AddressController 我有:
def create
@address = Address.new(address_params)
respond_to do |format|
if @address.save
format.html { redirect_to @address, notice: 'Estabelecimento criado com sucesso.' }
format.json { render action: 'show', status: :created, location: @address }
else
format.html { render action: 'new' }
format.json { render json: @address.errors, status: :unprocessable_entity }
end
end
end
这将创建新地址,但不会创建新机构。这可以自动创建链接Client 和Address 的新机构吗?在你问之前,我真的需要这个Establishment 模型。
【问题讨论】:
标签: ruby-on-rails many-to-many jointable