title: AssociationTypeMismatch Error on Ruby on Rails app
body: <p>I am having problems with my ruby on rails app. I have two models - 'patient' and 'address', a patient has one address, and an address belongs to a patient.</p>
  
  <p>Patient.rb</p>
  
  <pre><code>class Patient &lt; ActiveRecord::Base
    has_many :charge_slips
    has_one :address
  
    validates_presence_of :last_name
    validates_presence_of :first_name
    validates_presence_of :middle_name
  
  end
  </code></pre>
  
  <p>Address.rb</p>
  
  <pre><code>class Address &lt; ActiveRecord::Base
    belongs_to :patient
    validates_associated :patient
  end
  </code></pre>
  
  <p>Patient-controller.rb</p>
  
  <pre><code>class PatientController &lt; ApplicationController
    def index
      @title = "Outpatient Services - Patient"
      @today = Date.today.to_formatted_s(:long)
      @patients = Patient.find(:all)
    end
  
    def new
      @patient = Patient.new
      @address = Address.new
    end
  
    def create
      @patient = Patient.new(params[:patient])
      @patient.created_on = Date.today.to_formatted_s(:long)
  
      if @patient.save
        @address = Address.new(params[:address])
        @address.patient_id = @patient.id
        if @address.save
          redirect_to :action =&gt; 'index'
        else
          redirect_to :action =&gt; 'new'
        end
        redirect_to :action =&gt; 'index'
      else
        redirect_to :action =&gt; 'new'
      end
    end
  end
  </code></pre>
  
  <p>new.html.rb</p>
  
  <pre><code>&lt;%= content_tag('h3', 'Create New Patient') %&gt;
  &lt;hr&gt;
  &lt;% form_for @patient, :url =&gt; { :action =&gt; "create" } do |patient_form| -%&gt;
      &lt;%= error_messages_for :patient %&gt;
      &lt;%= patient_form.label :last_name, 'Last Name:' %&gt; &lt;%= patient_form.text_field :last_name, :size =&gt; 30 %&gt;&lt;br&gt;
      &lt;%= patient_form.label :first_name, 'First Name:' %&gt; &lt;%= patient_form.text_field :first_name, :size =&gt; 30 %&gt;&lt;br&gt;
      &lt;%= patient_form.label :middle_name, 'Middle Name:' %&gt; &lt;%= patient_form.text_field :middle_name, :size =&gt; 30 %&gt;&lt;br&gt;
  
      &lt;fieldset&gt;
          &lt;legend&gt;Patient's Permanent Address&lt;/legend&gt;
          &lt;%= error_messages_for :address %&gt;
          &lt;% patient_form.fields_for @address do |address_fields| -%&gt;
              &lt;%= address_fields.label :street_name, 'Street Name:' %&gt; &lt;%= address_fields.text_field :street_name %&gt;&lt;br&gt;
              &lt;%= address_fields.label :barangay, 'Barangay:' %&gt; &lt;%= address_fields.text_field :barangay %&gt;&lt;br&gt;
              &lt;%= address_fields.label :city_municipality, 'City/Municipality:' %&gt; &lt;%= address_fields.text_field :city_municipality %&gt;&lt;br&gt;
              &lt;%= address_fields.label :country, 'Country:' %&gt; &lt;%= address_fields.text_field :country %&gt;&lt;br&gt;
              &lt;%= address_fields.label :zip_cide, 'Zip Code:' %&gt; &lt;%= address_fields.text_field :zip_code %&gt;&lt;br&gt;
          &lt;% end -%&gt;
      &lt;/fieldset&gt;
  
      &lt;%= submit_tag "Add Patient" %&gt;
  &lt;% end -%&gt;
  </code></pre>
  
  <p>Everytime I add a new patient an error is thrown. Here is a part of the error:</p>
  
  <pre><code>ActiveRecord::AssociationTypeMismatch in PatientController#create
  
  Address(#31360520) expected, got HashWithIndifferentAccess(#23815500)
  
  RAILS_ROOT: C:/www/Outpatient Application Trace | Framework Trace | Full Trace
  
  C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/association_proxy.rb:263:in `raise_on_type_mismatch'
  C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/has_one_association.rb:52:in `replace'
  C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations.rb:1246:in `address='
  C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2740:in `send'
  C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2740:in `attributes='
  C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2736:in `each'
  C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2736:in `attributes='
  C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:2434:in `initialize'
  C:/www/Outpatient/app/controllers/patient_controller.rb:14:in `new'
  C:/www/Outpatient/app/controllers/patient_controller.rb:14:in `create'
  </code></pre>
  
  <p>I am new to RoR and would like to learn the language through practice. I want to know what might be wrong with the code. Thanks!</p>