multiple fields_for behavior

With the following code and result below, why does the cars_attributes index (i.e. name=owner[cars_attributes][>>>0<<<][color]) increment in the second fields_for block when only one car is built? Shouldn't it stay the same because only one car was built?

Is this standard behavior? If so, what is the standard way to force both indexes to be 0 (or increment together if there are more records)?

This is an over-simplified example. The reason I want two fields_for blocks is due the arrangement of partials in another complex form.

Much thanks!

Matt

# owners_controller.rb # ...   def new     @owner = Owner.new     @owner.cars.build

    respond_to do |format|       format.html # new.html.erb       format.xml { render :xml => @owner }     end   end # ...

# owner.rb class Owner < ActiveRecord::Base   has_many :cars   accepts_nested_attributes_for :cars end

# car.rb class Car < ActiveRecord::Base   belongs_to :owner end

# new.html.erb <%= form_for(@owner) do |f| %>   <% if @owner.errors.any? %>     <div id="error_explanation">       <h2><%= pluralize(@owner.errors.count, "error") %> prohibited this owner from being saved:</h2>

      <ul>       <% @owner.errors.full_messages.each do |msg| %>         <li><%= msg %></li>       <% end %>       </ul>     </div>   <% end %>

  <div class="field">     <%= f.label :name %><br />     <%= f.text_field :name %>   </div>

  <h2>Cars</h2>

  <%= f.fields_for :cars do |car_fields| %>     <div class="field">       <%= car_fields.label :color %><br />       <%= car_fields.text_field :color %>     </div>   <% end %>

I solved this using content_for. I put everything in the first fields_for but the fields I wanted to move to a different partial, I placed in the content_for block. See the example below.

Hope this helps,

Matt

# example   <%= f.fields_for :cars do |car_fields| %>     <div class="field">       <%= car_fields.label :color %><br />       <%= car_fields.text_field :color %><br />     </div>     <% content_for :plate_fields do %>       <div class="field">         <%= car_fields.label :plate %><br />         <%= car_fields.text_field :plate %><br />       </div>     <% end %>   <% end %>