title: rails 3 fields_for nested forms multiple records
body: <p>Maybe someone here will help me.</p>
  
  <p>I've got the following model <code>vehicle.rb</code>:</p>
  
  <pre><code>class Vehicle &lt; ActiveRecord::Base
    attr_accessible :parameters_attributes
    has_many :parameters, dependent: :destroy
    accepts_nested_attributes_for :parameters, reject_if: lambda {
    |attrs| attrs.all? {
      |tag, value|
        value.is_a?(Integer)
        tag.blank?
      }
    }
  end
  </code></pre>
  
  <p><code>vehicles_controller.rb</code></p>
  
  <pre><code>def new
    @vehicle = Vehicle.new
  end
  </code></pre>
  
  <p>and in my view <code>new.html.haml</code>:</p>
  
  <pre><code>= form_for [:admin, setup(@vehicle)], html: { multipart: true } do |f|
    %fieldset{ data: { hook: "new_vehicle" } }
      %legend{ align: "center" }
        = t(:new_vehicle)
    = render partial: 'shared_vehicle_fields', locals: { f: f }
    = f.field_container :size do
    = f.label :size
    %span.required *
    %br/
    = f.select :size, [t(:please_select) , "small", "medium", "large"], class: 'require'
    .parameter_fields
    %div.small_vehicle_parameters
      = f.fields_for :parameters do |pf|
        = render 'parameter_fields', f: pf, text: 'A1'
      = f.fields_for :parameters do |pf|
        = render 'parameter_fields', f: pf, text: 'A2'
    %div.medium_vehicle_parameters
    %div.large_vehicle_parameters
    %br/
    = f.submit t(:submit)
  </code></pre>
  
  <p>the <code>setup(@vehicle)</code> is the following helper:</p>
  
  <pre><code>def setup(vehicle)
    returning(vehicle) do |car|
      car.parameters.build if car.parameters.blank?
    end
  end
  </code></pre>
  
  <p>the partial <code>parameter_fields</code> is nothing special, but I'll show it just in case:</p>
  
  <pre><code>= f.label :tag, text
  = f.text_field :value, size: 4
  = f.hidden_field :tag, { value: text }
  %br/
  </code></pre>
  
  <p>Now, the problem is this:</p>
  
  <p>When I type in the correct values in any of the fields of the form everything is fine. It creates the models and records as I want them. But, if any of the validation doesn't pass the form is rendered again with error message. However, the parameter text_fields are multiplied. I get 2 <code>fields_for</code> with tag <code>A1</code> and 2 fields with <code>tag</code> <code>A2</code>. If I had 3 <code>fields_for</code>, then there would be 3 of each and so on.</p>
  
  <p>I know how many records I need to create in table (depends on vehicle size), if that helps.</p>
  
  <p>Help, anyone?</p>