title: Nested attributes for belongs_to association rails
body: <p>I have two models, Complaint and Company. Complaint <code>belongs_to</code> and <code>accepts_nested_attributes</code> for Company, and Company <code>has_many</code> Complaints.</p>
  
  <pre><code># Models
  
  class Complaint &lt; ActiveRecord::Base
    attr_accessible :complaint, :date, :resolved
  
    belongs_to :user, :class_name =&gt; 'User', :foreign_key =&gt; 'id'
    belongs_to :company, :class_name =&gt; 'Company', :foreign_key =&gt; 'id'
    has_many :replies
  
    accepts_nested_attributes_for :company
  
  end
  
  class Company &lt; ActiveRecord::Base
    attr_accessible :name
  
    has_many :complaints, :class_name =&gt; 'Complaint', :foreign_key =&gt; 'id'
    has_many :branches, :class_name =&gt; 'Branch', :foreign_key =&gt; 'id'
    belongs_to :industry
  
  end
  </code></pre>
  
  <p>In the Complaint Controller I try build a Company in the new method.</p>
  
  <pre><code># Complaint Controller
  
  class ComplaintsController &lt; ApplicationController
  ...
  def new
      @complaint = Complaint.new
      @complaint.build_company
  
      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @complaint }
      end
    end
  ...
  end
  </code></pre>
  
  <p>In the form I have added a field for adding a name attribute to the Company.</p>
  
  <pre><code># Complaint Form
  
  &lt;%= form_for(@complaint) do |f| %&gt;
    &lt;% if @complaint.errors.any? %&gt;
      &lt;div id="error_explanation"&gt;
        &lt;h2&gt;&lt;%= pluralize(@complaint.errors.count, "error") %&gt; prohibited this complaint from being saved:&lt;/h2&gt;
  
        &lt;ul&gt;
        &lt;% @complaint.errors.full_messages.each do |msg| %&gt;
          &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt;
        &lt;% end %&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  
    &lt;div class="field"&gt;
      &lt;%= f.label :complaint %&gt;&lt;br /&gt;
      &lt;%= f.text_area :complaint, :rows =&gt; 5 %&gt;
    &lt;/div&gt;
    &lt;div class="field"&gt;
      &lt;%= f.label :date %&gt;&lt;br /&gt;
      &lt;%= f.datetime_select :date %&gt;
    &lt;/div&gt;
  
    &lt;% if current_user.try(:admin?) %&gt;
      &lt;div class="field"&gt;
        &lt;%= f.label :resolved %&gt;&lt;br /&gt;
        &lt;%= f.check_box :resolved %&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  
    &lt;%= fields_for :company do |company| %&gt;
      &lt;div class="field"&gt;
        &lt;%= company.label :name, 'Company' %&gt;
        &lt;%= company.text_field :name %&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  
    &lt;div class="actions"&gt;
      &lt;%= f.submit %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>The form submits but only the Complaint is saved. The user input for Company is disregarded. Why won't this create a new Company?</p>