[英]accepts_nested_attributes_for and second-order associations, nested forms
I have following models with associations: 我有以下具有关联的模型:
class Order < ActiveRecord::Base
has_many :guests
has_many :customers, :through => :guests
accepts_nested_attributes_for :customers
end
class Customer < ActiveRecord::Base
has_many :guests
has_many :orders, :through => :guests
has_many :slips
accepts_nested_attributes_for :slips
end
class Slip < ActiveRecord::Base
belongs_to :order
belongs_to :customer
end
class Guest < ActiveRecord::Base
belongs_to :order
belongs_to :customer
end
My nested form looks like this: 我的嵌套表单如下所示:
<!-- general form -->
<%= form_for(@order) do |f| %>
<% f.fields_for :customers do |builder| %>
<%= render "customer_fields", :f => builder %>
<% end %>
<%= f.submit %>
<% end %>
<!-- partial customer_fields -->
<p>
<%= f.label :name%><%= f.text_field :name %>
<% f.fields_for :slips do |builder| %>
<%= render "slip_fields", :f => builder %>
<% end %>
</p>
<!-- partial slip_fields -->
<p><%= f.label :quantity%><%= f.text_field :quantity %></p>
With this setup saving an order works as expected, but I need the order_id to be saved with the slip, so I have a reference between order <-> slip. 通过此设置,保存订单可以按预期方式工作,但是我需要将order_id与发票一起保存,因此我在订单<->发票之间有一个引用。 With this setup I loose the reference.
通过这种设置,我失去了参考。 I can get all associated customers, but I'll get all associated slips of the customer related to the order or not.
我可以获取所有关联的客户,但是可以获取与订单相关或不相关的所有与客户相关的发票。
Here the fields of my models: Order -> id 这是我模型的字段:订单-> id
Customer -> id 客户-> ID
Guest -> id, order_id, customer_id 访客-> id,order_id,customer_id
Slip -> id, order_id, customer_id 单据-> id,order_id,customer_id
The result of an order should look like this 订单结果应如下所示
I've no idea how to accomplish this. 我不知道该怎么做。
As far as you can't return order_id
for order that isn't exist you can do this hook (I haven't test it, so you'll maybe need to fix it) 至于您不能为不存在的订单返回
order_id
,则可以执行此挂接操作(我尚未对其进行测试,因此您可能需要对其进行修复)
def create
customers = params[:order].delete :customers_attributes
@order = Order.new params[:order]
if @order.save
customers.each{|c| c[:slips_attributes].each{|s| s[:order_id] = @order.id} }
@order.customers_attributes = customers
@order.save
end
end
def update
@order = Order.find params[:id]
params[:order][:customers_attributes].each{|c| c[:slips_attributes].each{|s| s[:order_id] = @order.id} }
@order = Order.update_attributes params[:order]
@order.save
end
Also you'd better to remove all this logic into your model and you can dry
it a little. 另外,您最好将所有这些逻辑都删除到模型中,然后将其
dry
一下。 This only about understanding an approach. 这仅与了解方法有关。
UPD for your ID collisions. UPD用于您的ID冲突。 It is only a scetch again
只是一个scetch
def create
customers = params[:order].delete :customers_attributes
@order = Order.new params[:order]
@order.customer_ids = customers.inject([]){|a,h| a << h[:b] if h[:b]; a}
if @order.save
customers.each{|c| c[:slips_attributes].each{|s| s[:order_id] = @order.id} }
@order.customers_attributes = customers
@order.save
end
end
def update
@order = Order.find params[:id]
@order.customer_ids = params[:order][:customers_attributes].inject([]){|a,h| a << h[:b] if h[:b]; a}
params[:order][:customers_attributes].each{|c| c[:slips_attributes].each{|s| s[:order_id] = @order.id} }
@order = Order.update_attributes params[:order]
@order.save
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.