title: accepts_nested_attributes_for and second-order associations, nested forms
body: <p>I have following models with associations:</p>
  
  <pre><code>class Order &lt; ActiveRecord::Base
    has_many :guests
    has_many :customers, :through =&gt; :guests
    accepts_nested_attributes_for :customers
  end
  
  class Customer &lt; ActiveRecord::Base
     has_many :guests
     has_many :orders, :through =&gt; :guests
     has_many :slips
     accepts_nested_attributes_for :slips
  end
  
  class Slip &lt; ActiveRecord::Base
     belongs_to :order
     belongs_to :customer
  end
  
  class Guest &lt; ActiveRecord::Base
    belongs_to :order
    belongs_to :customer
  end
  </code></pre>
  
  <p>My nested form looks like this:</p>
  
  <pre><code>&lt;!-- general form --&gt;
  &lt;%= form_for(@order) do |f| %&gt;
      &lt;% f.fields_for :customers do |builder| %&gt;
          &lt;%= render "customer_fields", :f =&gt; builder %&gt;
      &lt;% end %&gt;
      &lt;%= f.submit %&gt;
  &lt;% end %&gt;
  
  &lt;!-- partial customer_fields --&gt;
  &lt;p&gt;
      &lt;%= f.label :name%&gt;&lt;%= f.text_field :name %&gt;
      &lt;% f.fields_for :slips do |builder| %&gt;
          &lt;%= render "slip_fields", :f =&gt; builder %&gt;
      &lt;% end %&gt;
  &lt;/p&gt;
  
  &lt;!-- partial slip_fields --&gt;
  &lt;p&gt;&lt;%= f.label :quantity%&gt;&lt;%= f.text_field :quantity %&gt;&lt;/p&gt;
  </code></pre>
  
  <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 &lt;-> 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.</p>
  
  <hr>
  
  <p>Here the fields of my models:
  Order -> id<br>
  Customer -> id<br>
  Guest -> id, order_id, customer_id<br>
  Slip -> id, order_id, customer_id</p>
  
  <hr>
  
  <p>The result of an order should look like this</p>
  
  <ul>
  <li>Order<br>
  <ol>
  <li>Customer A
  <ul>
  <li>Slip 1</li>
  <li>Slip 2</li>
  </ul></li>
  <li>Customer B
  <ul>
  <li>Slip 1</li>
  <li>Slip 2</li>
  </ul></li>
  <li>Customer A
  <ul>
  <li>Slip 1</li>
  <li>Slip 2</li>
  <li>Slip 3</li>
  </ul></li>
  </ol></li>
  </ul>
  
  <hr>
  
  <p>I've no idea how to accomplish this.</p>