title: saving to two different models - accepts_nested_attributes_for not working
body: <p>EDITED: code below reflects last suggestions from your answers...</p>
  
  <p>I'm having trouble with saving to two different models, from one form... I tried to follow rails casts #196 and looked at several other examples here at stackoverflow but I haven't figured out how to do this because my case is a little bit different...</p>
  
  <p>So, I have two models:</p>
  
  <pre><code>class Patient &lt; ActiveRecord::Base
    attr_accessible :date_of_birth, :patient_name
  
    has_many :samples
  end
  
  class Sample &lt; ActiveRecord::Base
    attr_accessible :approved, :patientID, :result, patient_attributes: [:patient_name, :date_of_birth]
  
    belongs_to :patient
    accepts_nested_attributes_for :patient
  end
  </code></pre>
  
  <p>What I found on most of the examples I saw, including the railscasts, is to create a new sample inside the patient form, and the "accepts_nestes_attributes_for" is inside the "patient" model... But what I want to do is exactly the opposite, that is, creating a new patient inside the new sample form.</p>
  
  <p>Here're my views, I have a normal form for the sample and then a partial for the patient part:</p>
  
  <p>_form.html.erb</p>
  
  <pre><code>&lt;%= form_for(@sample) do |f| %&gt;
    &lt;% if @sample.errors.any? %&gt;
      &lt;div id="error_explanation"&gt;
        &lt;h2&gt;&lt;%= pluralize(@sample.errors.count, "error") %&gt; prohibited this sample from being saved:&lt;/h2&gt;
  
        &lt;ul&gt;
        &lt;% @sample.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 :result %&gt;&lt;br /&gt;
      &lt;%= f.text_area :result %&gt;
    &lt;/div&gt;
    &lt;div class="field"&gt;
      &lt;%= f.label :approved %&gt;&lt;br /&gt;
      &lt;%= f.check_box :approved %&gt;
    &lt;/div&gt;
    &lt;div&gt;&lt;%= render 'patient', f: f %&gt;&lt;/div&gt;
    &lt;div class="actions"&gt;
      &lt;%= f.submit %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>patient.html.erb </p>
  
  <pre><code>&lt;%= f.fields_for :patient do |p| %&gt;
  &lt;div class="field"&gt;
      &lt;%= p.label :patient_name %&gt;&lt;br /&gt;
      &lt;%= p.text_field :patient_name %&gt;
  &lt;/div&gt;
  &lt;div class="field"&gt;
      &lt;%= p.label :date_of_birth %&gt;&lt;br /&gt;
      &lt;%= p.date_select :date_of_birth %&gt;
  &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>When I click the submit button everything seems to work, but nothing is saved into the patients table.</p>
  
  <p>Here's the log:</p>
  
  <pre><code>Started POST "/samples" for 127.0.0.1 at 2013-10-12 23:32:03 +0100
  Processing by SamplesController#create as HTML
    Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"13OKZ8DaGJ4zTb35q+ymSzx7r+Ipxou1u+XrR4jtyeI=", "sample"=&gt;{"result"=&gt;"teste 3", "approved"=&gt;"0"}, "patient"=&gt;{"patient_name"=&gt;"Joao", "date_of_birth(1i)"=&gt;"2013", "date_of_birth(2i)"=&gt;"10", "date_of_birth(3i)"=&gt;"10"}, "commit"=&gt;"Create Sample"}
     (0.1ms)  begin transaction
    SQL (0.5ms)  INSERT INTO "samples" ("approved", "created_at", "patientID", "result", "updated_at") VALUES (?, ?, ?, ?, ?)  [["approved", false], ["created_at", Sat, 12 Oct 2013 22:32:03 UTC +00:00], ["patientID", nil], ["result", "teste 3"], ["updated_at", Sat, 12 Oct 2013 22:32:03 UTC +00:00]]
     (2.4ms)  commit transaction
  Redirected to http://localhost:3000/samples/3
  Completed 302 Found in 6ms (ActiveRecord: 2.9ms)
  </code></pre>
  
  <p>As you can see in the log, it's only saving to the sample table. Can I use accepts_nested_attributes_for this way? Is there a way to achieve what I want? Or should I try a different approach?</p>
  
  <p>UPDATE:
  Here's the code for my patient and sample controllers:</p>
  
  <p>patients_controller.rb</p>
  
  <pre><code>def new
      @patient = Patient.new
  
      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @patient }
      end
  end
  
  def create
      @patient = Patient.new(params[:patient])
  
      respond_to do |format|
        if @patient.save
          format.html { redirect_to @patient, notice: 'Patient was successfully created.' }
          format.json { render json: @patient, status: :created, location: @patient }
        else
          format.html { render action: "new" }
          format.json { render json: @patient.errors, status: :unprocessable_entity }
        end
      end
  end
  </code></pre>
  
  <p>samples_controller.rb</p>
  
  <pre><code>def new
      @sample = Sample.new
      @sample.build_patient #suggested by El Key
  
      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @sample }
      end
  end
  
  def create
      @sample = Sample.new(params[:sample])
  
      respond_to do |format|
        if @sample.save
          format.html { redirect_to @sample, notice: 'Sample was successfully created.' }
          format.json { render json: @sample, status: :created, location: @sample }
        else
          format.html { render action: "new" }
          format.json { render json: @sample.errors, status: :unprocessable_entity }
        end
      end
  end
  </code></pre>
  
  <p>Here's the updated log after I follow El Key suggestions:</p>
  
  <pre><code>Started POST "/samples" for 127.0.0.1 at 2013-10-17 00:20:05 +0100
  Processing by SamplesController#create as HTML
    Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"NZXQUdd8tQc206LdEuYF5iO5+89wlfza0VbbvgBGNuI=", "sample"=&gt;{"result"=&gt;"teste 9", "patientID"=&gt;"", "approved"=&gt;"0", "patient_attributes"=&gt;{"patient_name"=&gt;"Sergio", "date_of_birth(1i)"=&gt;"2013", "date_of_birth(2i)"=&gt;"10", "date_of_birth(3i)"=&gt;"2"}}, "commit"=&gt;"Create Sample"}
  Completed 500 Internal Server Error in 1ms
  
  ActiveModel::MassAssignmentSecurity::Error - Can't mass-assign protected attributes: patient_attributes:
  </code></pre>
  
  <p>As you can see, now I have a problem for "can't mass-assign protected attributes", but shouldn't this work as I have the "patient_attributes" on the sample model?</p>
  
  <p>After I solve this I will try to check first if the patient exists or not before committing and try to do as SAYT on the patient's name... but that's a future step!</p>
  
  <h2>Thanks for your help</h2>
  
  <p>After following El-Key advise and suggestions, I found the solution</p>
  
  <p><strong>Update, Final Code - My solution</strong></p>
  
  <p>models/samples.rb</p>
  
  <pre><code>class Sample &lt; ActiveRecord::Base
    attr_accessible :approved, :patient_id, :result, :patient_attributes
  
    belongs_to :patient
    accepts_nested_attributes_for :patient
  end
  </code></pre>
  
  <p>models/patient.rb</p>
  
  <pre><code>class Patient &lt; ActiveRecord::Base
    attr_accessible :date_of_birth, :patient_name 
    has_many :samples
  end
  </code></pre>
  
  <p>controllers/samples_controller</p>
  
  <pre><code>def new
      @sample = Sample.new
      @sample.build_patient
  
      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @sample }
      end
  end
  
  def create
      @sample = Sample.new(params[:sample])
  
      respond_to do |format|
        if @sample.save
          format.html { redirect_to @sample, notice: 'Sample was successfully created.' }
          format.json { render json: @sample, status: :created, location: @sample }
        else
          format.html { render action: "new" }
          format.json { render json: @sample.errors, status: :unprocessable_entity }
        end
      end
  end
  </code></pre>
  
  <p>views/samples/_form.html.erb</p>
  
  <pre><code>&lt;%= form_for(@sample) do |f| %&gt;
    &lt;% if @sample.errors.any? %&gt;
      &lt;div id="error_explanation"&gt;
        &lt;h2&gt;&lt;%= pluralize(@sample.errors.count, "error") %&gt; prohibited this sample from being saved:&lt;/h2&gt;
  
        &lt;ul&gt;
        &lt;% @sample.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 :result %&gt;&lt;br /&gt;
      &lt;%= f.text_area :result %&gt;
    &lt;/div&gt;
      &lt;div class="field"&gt;
          &lt;%= f.label :patient_id, 'Patient ID' %&gt;&lt;br /&gt;
          &lt;%= f.text_area :patient_id %&gt;
      &lt;/div&gt;
    &lt;div class="field"&gt;
      &lt;%= f.label :approved %&gt;&lt;br /&gt;
      &lt;%= f.check_box :approved %&gt;
    &lt;/div&gt;
  
    &lt;div&gt;&lt;%= render 'patient', f: f %&gt;&lt;/div&gt;
    &lt;div class="actions"&gt;
      &lt;%= f.submit %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>views/samples/_patient.html.erb</p>
  
  <pre><code>&lt;%= f.fields_for :patient do |p| %&gt;
  &lt;div class="field"&gt;
         &lt;%= p.label :patient_name %&gt;&lt;br /&gt;
         &lt;%= p.text_field :patient_name %&gt;
  &lt;/div&gt;
  &lt;div class="field"&gt;
         &lt;%= p.label :date_of_birth %&gt;&lt;br /&gt;
         &lt;%= p.date_select :date_of_birth %&gt;
  &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>