我有以下型號協會: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與slip一起保存,所以我在< - > slip之間有一個參考。有了這個設置,我放棄了參考。我可以獲得所有關聯的客戶,但是我會得到與訂單相關的客戶的所有相關單據。
這裏我的模型的字段: 訂單 - > ID
客戶 - > ID
嘉賓 - > ID,ORDER_ID,CUSTOMER_ID
滑 - > ID,ORDER_ID,CUSTOMER_ID
訂單的結果應該看起來像這樣
- 爲了
- 客戶A
- 滑1
- 滑2
- 客戶B
- 滑1
- 滑2
- 客戶A
- 滑1
- 滑2
- 滑3
- 客戶A
我不知道如何做到這一點。
我可以想象如何爲存在@order,但對於新秩序它是非常複雜的。你可以在這裏使用old school虛擬屬性來嵌套資源,但不能使用現代accept_nested_attributes_for' – fl00r 2011-04-11 14:03:07
也許有一個鉤子,我可以將order_id傳遞給滑動模型? – wdbmh 2011-04-11 15:38:40
我已添加評論來解釋解決方案 – fl00r 2011-04-11 16:42:35