title: Rails Nested attributes being duplicated
body: <p>This problem has beaten me. I've  been at it all weekend but can't figure out what is happening.</p>
  
  <p>When I create a new <code>employee</code> object, I also want to create multiple new <code>ee_pay</code> records using <code>accepts_nested_attributes_for :ee_pay</code> in my <code>employee</code> model.</p>
  
  <p>Creation of <code>ee_pay</code> records for new employees is dependent on the amount of <code>company_pay</code> objects that exist for the company that the employee belongs to. So in the case below there's 2 <code>company_pay</code> objects -> "Basic" [id:2] and "Time + 1/2" [id:3] (returned in the <code>before_action</code>) and I want to create an <code>ee_pay</code> for each of these for the employee in question</p>
  
  <p>Here's the code:-</p>
  
  <blockquote>
    <p><strong>employees_controller.rb</strong></p>
  </blockquote>
  
  <pre><code> before_action :set_company_pay_types, only: [:new, :update, :edit]
  
          def new
              @ee_pay_types = []
              @taxable_pays.each do |pt|
                @ee_pay_types &lt;&lt; @employee.ee_pay.build(company_pay_id: pt.id)
              end
          end                 
  
          private
            def set_company_pay_types
                @taxable_pays = CompanyPay.where(['company_id = ? AND pay_sub_head_id = ? AND description &lt;&gt; ?', current_company.id, 1, "Salary"]).order(:id)
            end
  
            def employee_params
              params.require(:employee).permit(....[multiple other fields]..., address_attributes:[:id, :line1, :line2, :line3, :line4, :postcode, :country], ee_pay_attributes:[:id, :employee_id, :company_pay_id, :amount, :rate])
            end
  </code></pre>
  
  <blockquote>
    <p><strong>views/employees/_form</strong></p>
  </blockquote>
  
  <pre><code>&lt;div&gt;&lt;strong&gt;Hourly Pay Types&lt;/strong&gt;&lt;/div&gt;
  &lt;div class="form_spacer"&gt;&lt;/div&gt;
  &lt;div class="row"&gt;
     &lt;div class="col-md-3"&gt;  
        &lt;div class="form_indent1"&gt;&lt;div class="form_label"&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/div&gt;&lt;/div&gt;           
     &lt;/div&gt; 
     &lt;div class="col-md-3"&gt;  
        &lt;div class="form_indent1"&gt;&lt;div class="form_label"&gt;&lt;strong&gt;Amount&lt;/strong&gt;&lt;/div&gt;&lt;/div&gt;           
     &lt;/div&gt;
     &lt;div class="col-md-2"&gt;  
        &lt;div class="form_indent1"&gt;&lt;div class="form_label"&gt;&lt;strong&gt;Units&lt;/strong&gt;&lt;/div&gt;&lt;/div&gt;           
     &lt;/div&gt;                
     &lt;div class="col-md-4"&gt;  
        &lt;div class="form_indent1"&gt;&lt;div class="form_label"&gt;&lt;strong&gt;Rate&lt;/strong&gt;&lt;/div&gt;&lt;/div&gt;           
     &lt;/div&gt; 
     &lt;div&gt;
               &lt;%= debug @ee_pay_types %&gt; 
               &lt;% @ee_pay_types.each do |ee_pay| %&gt;
                   &lt;%= f.fields_for :ee_pay do |builder| %&gt;                                   
                   &lt;%= builder.hidden_field :company_pay_id %&gt;     
                   &lt;div class="col-md-3"&gt;
                       &lt;div class="form_indent1"&gt;&lt;div class="form_indent1"&gt;&lt;%= ee_pay.company_pay.description %&gt;&lt;/div&gt;&lt;/div&gt;
                       &lt;div class="form_spacer"&gt;&lt;/div&gt;
                   &lt;/div&gt; 
                   &lt;div class="col-md-3"&gt;
                       &lt;div class="form_indent1"&gt;&lt;%= builder.text_field :amount, class: "number_input" %&gt;&lt;/div&gt;
                       &lt;div class="form_spacer"&gt;&lt;/div&gt;
                   &lt;/div&gt;  
                   &lt;div class="col-md-2"&gt;
                       &lt;div class="form_indent1"&gt;&lt;%= ee_pay.company_pay.units %&gt;&lt;/div&gt;
                       &lt;div class="form_spacer"&gt;&lt;/div&gt;
                   &lt;/div&gt;  
                   &lt;div class="col-md-4"&gt;  
                       &lt;div class="form_indent1"&gt;&lt;%= builder.text_field :rate, class: "number_input" %&gt;&lt;/div&gt;
                       &lt;div class="form_spacer"&gt;&lt;/div&gt;&lt;br /&gt;
                   &lt;/div&gt;                             
  
                   &lt;% end %&gt; 
               &lt;% end %&gt; 
     &lt;/div&gt; 
     &lt;/div&gt;
  </code></pre>
  
  <blockquote>
    <p><strong>Output from employees/new</strong></p>
  </blockquote>
  
  <p>When I go to create a new employee record, I'm getting the following output for the <code>ee_pay</code> objects. It turns out the code is duplicating each one in the following format:-</p>
  
  <pre><code>*description*        *company_pay_id*    
  Basic                   2
  Basic                   3
  Time &amp; 1/2              2
  Time &amp; 1/2              3
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/9OAlY.jpg" alt="output from employees/new"></p>
  
  <p>It looks to me like this line <code>@ee_pay_types &lt;&lt; @employee.ee_pay.build(company_pay_id: pt.id)</code> in the <code>employees_controller</code> is building two new <code>ee_pay</code> objects (that are output in the debug in the picture above). Then in the view, it's iterating over these two and for each one, creating two new <code>ee_pay</code> objects. I think this is what's happening but I could be completely wring. I'm lost at this stage and I've no idea how to fix it. </p>
  
  <p>Hopefully, someone can point me in the right direction on how to solve it. It's probably something very obvious that I'm missing.</p>
  
  <p>Thanks for looking</p>
  
  <blockquote>
    <p><strong>Edit 1</strong></p>
  </blockquote>
  
  <p>Adding the models as requested </p>
  
  <blockquote>
    <p><strong>models/ee_pay</strong></p>
  </blockquote>
  
  <pre><code>    class EePay &lt; ActiveRecord::Base
          belongs_to :employee
          belongs_to :company_pay
      end
  </code></pre>
  
  <blockquote>
    <p><strong>model/company_pay</strong></p>
  </blockquote>
  
  <pre><code>    class CompanyPay &lt; ActiveRecord::Base
          belongs_to :pay_sub_head
          belongs_to :company
          has_many :ee_pay
      end
  </code></pre>
  
  <blockquote>
    <p><strong>model/employee</strong></p>
  </blockquote>
  
  <pre><code>    class Employee &lt; ActiveRecord::Base
          belongs_to :company
          belongs_to :address
          accepts_nested_attributes_for :address
          has_many :ee_pay
          accepts_nested_attributes_for :ee_pay
      end
  </code></pre>
  
  <p>Yann suggested in the comments changing the <code>employee_controller</code> to this</p>
  
  <pre><code>    @ee_pay_types = []
      @taxable_pays.each do |pt|
          @employee.ee_pay.build(company_pay_id: pt.id)
      end
  </code></pre>
  
  <p>and removing the iteration over <code>@ee_pay_types</code> in the view, which now looks like (I changed any reference to <code>ee_pay</code> to <code>builder</code>):- </p>
  
  <pre><code>    &lt;div&gt;
              &lt;%= debug @ee_pay_types %&gt;  
              &lt;%= f.fields_for :ee_pay do |builder| %&gt;                                    
                  &lt;%= builder.hidden_field :company_pay_id %&gt;     
  
                  &lt;div class="col-md-3"&gt;
                      &lt;div class="form_indent1"&gt;&lt;div class="form_indent1"&gt;&lt;%= builder.company_pay.description %&gt;&lt;/div&gt;&lt;/div&gt;
                      &lt;div class="form_spacer"&gt;&lt;/div&gt;
                  &lt;/div&gt; 
                      &lt;div class="col-md-3"&gt;
                      &lt;div class="form_indent1"&gt;&lt;%= builder.text_field :amount, class: "number_input" %&gt;&lt;/div&gt;
                      &lt;div class="form_spacer"&gt;&lt;/div&gt;
                  &lt;/div&gt;  
                      &lt;div class="col-md-2"&gt;
                      &lt;div class="form_indent1"&gt;&lt;%= builder.company_pay.units %&gt;&lt;/div&gt;
                      &lt;div class="form_spacer"&gt;&lt;/div&gt;
                  &lt;/div&gt;  
                      &lt;div class="col-md-4"&gt;  
                      &lt;div class="form_indent1"&gt;&lt;%= builder.text_field :rate, class: "number_input" %&gt;&lt;/div&gt;
                      &lt;div class="form_spacer"&gt;&lt;/div&gt;&lt;br /&gt;
                  &lt;/div&gt;
              &lt;% end %&gt; 
      &lt;/div&gt; 
  </code></pre>
  
  <p>But this gives me the error:-</p>
  
  <pre><code>undefined method `company_pay' for #&lt;ActionView::Helpers::FormBuilder:0x007f47d5649118&gt;
  </code></pre>
  
  <p>To me it looks like I can't access the <code>company_pay</code> of the <code>ee_pay</code> being created. Anyone any ideas?</p>
  
  <blockquote>
    <p><strong>Edit 2 - Solved</strong></p>
  </blockquote>
  
  <p>After making the edits suggested by Yan Foto, I was then able to access the <code>company_pay</code> of the <code>ee_pay</code> being created using <code>builder.object.company_pay.description</code>. Thanks again Yan.</p>