Accepts_nested_attributes_for 和二阶关联、嵌套形式
我有以下具有关联的模型:
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
我的嵌套表单如下所示:
<!-- 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>
通过此设置,保存订单可以按预期工作,但我需要将 order_id 与单据一起保存,因此我在订单 <-> 之间有一个引用。滑。通过这种设置,我失去了参考。我可以获取所有关联的客户,但我将获取与订单相关或不相关的客户的所有关联单据。
这是我的模型的字段: 订单->编号
客户->编号
访客-> id、order_id、customer_id
滑倒-> id, order_id, customer_id
订单的结果应如下所示
- Order
- 客户 A
- 单据 1
- 单据 2
- 客户 B
- 单据 1
- 单据 2
- 客户 A
- 单据 1
- 单据 2
- 单据3
- 客户 A
我不知道如何实现这一点。
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. 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
Customer -> id
Guest -> id, order_id, customer_id
Slip -> id, order_id, customer_id
The result of an order should look like this
- Order
- Customer A
- Slip 1
- Slip 2
- Customer B
- Slip 1
- Slip 2
- Customer A
- Slip 1
- Slip 2
- Slip 3
- Customer A
I've no idea how to accomplish this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
至于您无法为不存在的订单返回
order_id
,您可以执行此挂钩(我还没有测试它,所以您可能需要修复它)另外您也可以最好将所有这些逻辑删除到您的模型中,您可以将其稍微干燥一下。这仅涉及理解一种方法。
针对您的 ID 冲突UPD。这又只是一个草图
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)Also you'd better to remove all this logic into your model and you can
dry
it a little. This only about understanding an approach.UPD for your ID collisions. It is only a scetch again