title: Rails 3 Nested Model Form, 2 levels deep using accepts_nested_attributes_for
body: <p>My nested model form is working great on the first level deep.  But I was under the impression that you could go many levels deep using accepts_nested_attributes_for.  But when I try the code below, the "Image" attributes are attached to the top level "Question" model and it breaks upon form submission with an unknown attribute "Image" error.  </p>
  
  <p>I could do the inserts all by hand using the form data but if Rails can handle it automatically, it would be better for obvious reasons.</p>
  
  <p>What am I doing wrong?  I tried changing |af| in the "fields for :image do" to its own unique name but it didn't have any effect.</p>
  
  <p>Models:</p>
  
  <pre><code>class Question &lt; ActiveRecord::Base
    has_one :answer
    accepts_nested_attributes_for :answer
  end
  
  class Answer &lt; ActiveRecord::Base
    belongs_to :question
    has_one :image
    accepts_nested_attributes_for :image
  end
  
  class Image &lt; ActiveRecord::Base
    belongs_to :answer
  end
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>def new
      @question = Question.new
      answer = @question.build_answer
      image = answer.build_image
  
      @case_id = params[:id]
  
      render :layout =&gt; 'application', :template =&gt; '/questions/form' 
  end
  
  def create
    question_data = params[:question]
    @question = Question.new(question_data)
    if @question.save
    ...
  end
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>= form_for @question, :html =&gt; {:multipart =&gt; true} do |f|
  
    = f.label :text, "Question Text:"
    = f.text_area :text, :rows =&gt; 7
  
    %br
    %br
  
    =f.fields_for :answer, do |af|
      = af.label :body, "Answer Text:"
      = af.text_area :body, :rows =&gt; 7
  
      %br
      %br
  
      = f.fields_for :image do |af|
        = af.label :title, "Image Title:"
        = af.text_field :title
  
        %br
  
        = af.label :file, "Image File:"
        = af.file_field :file
  
        %br
  
        = af.label :caption, "Image Caption:"
        = af.text_area :caption, :rows =&gt; 7
  
    = hidden_field_tag("case_id", value = @case_id)
  
    = f.submit
  </code></pre>