title: Nested field model not saving the parent association in Rails
body: <p>I have a <code>SaleQualifier</code> Model in my rails app that <code>has_many answers</code>. When trying to save both the <code>sale_qualifier</code> and the <code>answer</code> I'm getting the error <code>Answers sale_qualifier can't be blank</code> and I can't work out what the issue is, as I've got these nested attributes working in other forms on the app.</p>
  
  <p>The <code>SaleQualifier</code> is shown on the <code>SalesOpportunity</code> page, so I first define this in the <code>SalesOpportunity show</code> action:</p>
  
  <pre><code>  def show
     @sales_opportunity = SalesOpportunity.includes(:company, :user, :timeline_events, :sale_contacts, :swots, :sale_competitors).find(params[:id])
     @sale_qualifier = SaleQualifier.new(sales_opportunity_id: params[@sales_opportunity.id])
     @answer = @sale_qualifier.answers.build
   end
  </code></pre>
  
  <p>As you can see I'm building the association in the show controller, which ought to then update via the <code>SaleQualifier create</code> action. In the SaleQualifier controller I have set the following:</p>
  
  <pre><code>    def sale_qualifier_params
       params.require(:sale_qualifier).permit(:question_id, :answer_id, :next_question_id, :answered, :sales_opportunity_id, answers_attributes: [:answer_text, :sale_qualifier_id])
     end
  </code></pre>
  
  <p>My Sale Qualifier code:</p>
  
  <pre><code>class SaleQualifier &lt; ActiveRecord::Base
   has_many :answers, :inverse_of =&gt; :sale_qualifier
   accepts_nested_attributes_for :answers
   belongs_to :sales_opportunity
   validates :sales_opportunity_id, presence: true
  end
  </code></pre>
  
  <p>My Answer.rb:</p>
  
  <pre><code>class Answer &lt; ActiveRecord::Base
   validates :answer_text, presence: true
   belongs_to :sale_qualifier
   validates :sale_qualifier_id, presence: true
  end
  </code></pre>
  
  <p>My view form:</p>
  
  <pre><code>&lt;div class="panel panel-default"&gt;
   &lt;%= form_for(@sale_qualifier) do |f| %&gt;
   &lt;% if @sale_qualifier.errors.any? %&gt;
    &lt;div id="error_explanation"&gt;
      &lt;h2&gt;&lt;%= pluralize(@sale_qualifier.errors.count, "error") %&gt; prohibited this answer from being saved:&lt;/h2&gt;
      &lt;ul&gt;
      &lt;% @sale_qualifier.errors.full_messages.each do |message| %&gt;
        &lt;li&gt;&lt;%= message %&gt;&lt;/li&gt;
      &lt;% end %&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  &lt;div class="panel-body"&gt;
    &lt;div class="col-sm-6"&gt;
      &lt;h2&gt;&lt;%= @question.question_text %&gt;&lt;/h2&gt;
    &lt;/div&gt;
    &lt;div class="col-sm-6"&gt;
      &lt;div class="form-group"&gt;
        &lt;%= f.hidden_field :sales_opportunity_id, :value =&gt; @sales_opportunity.id %&gt;
      &lt;/div&gt;
      &lt;%= f.fields_for :answers do |answer| %&gt;
          &lt;%= answer.text_area :answer_text, :placeholder =&gt; "Enter your answer" %&gt;
      &lt;% end %&gt;
      &lt;div class="actions"&gt;
        &lt;%= f.submit "Submit", class: "btn btn-large btn-success"%&gt;
      &lt;/div&gt;
      &lt;% end %&gt;
    &lt;/div&gt;
   &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>All fields show correctly on the view. This is the log file from the submit button click:</p>
  
  <pre><code>Started POST "/sale_qualifiers" for 127.0.0.1 at 2016-01-13 13:12:57 +0800
  Processing by SaleQualifiersController#create as HTML
    Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"0NdiFqK9O2fqh7TvDamrUL4jeQ6otcoQGhBZX9DlaSg=", "sale_qualifier"=&gt;{"sales_opportunity_id"=&gt;"77", "answers_attributes"=&gt;{"0"=&gt;{"answer_text"=&gt;"An Answer"}}}, "commit"=&gt;"Submit"}
  </code></pre>
  
  <p>The error returns "Answers sale_qualifier can't be blank".</p>
  
  <p>I'm sure there's some straightforward error that I can't see in my code, if anyone can help me find it I'd appreciate it.</p>