title: AssociationTypeMismatch (Object expected, got HashWithIndifferentAccess) in Rails app
body: <p>I'm getting a AssociationTypeMismatch Error and I'm not sure where I'm making a mistake. I'm pretty new to Rails so I'm guessing I'm making some silly mistake. I've checked my syntax and compared it against <a href="https://stackoverflow.com/questions/1169813/associationtypemismatch-error-on-ruby-on-rails-app">AssociationTypeMismatch Error on Ruby on Rails app</a></p>
  
  <p>... but I still can't seem to catch the error.</p>
  
  <p>Here's my models</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    attr_accessible :email, :full_name, :password, :password_confirmation, :preference
    has_secure_password
  
    validates_uniqueness_of :email
    validates_presence_of :full_name
  
    has_one :preference, :dependent =&gt; :destroy
  
    accepts_nested_attributes_for :preference
  end
  
  class Preference &lt; ActiveRecord::Base
    attr_accessible :background_fill, :background_position, :layout
  
    belongs_to :user
  end
  </code></pre>
  
  <p>Here's my controller:</p>
  
  <pre><code>def new
    @user = User.new
    @user.build_preference
  end
  
  def create
    @user = User.new(params[:user])
    if @user.save
      session[:user_id] = @user.id
      redirect_to root_url, notice: "Thanks for signing up!"
    else
      render "new"
    end
  end
  </code></pre>
  
  <p>Here's my view:</p>
  
  <pre><code>&lt;%= form_for @user do |f| %&gt;
    &lt;% if @user.errors.any? %&gt;
    &lt;div class="error_messages"&gt;
      &lt;h2&gt;There's an error!&lt;/h2&gt;
      &lt;ul&gt;
        &lt;% @user.errors.full_message.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;%= f.label :full_name %&gt;
    &lt;%= f.text_field :full_name, :class =&gt; "target", :placeholder =&gt; "Your full name", :maxlength =&gt; "55", :autofocus =&gt; "autofocus" %&gt;
    &lt;%= f.label :email %&gt;
    &lt;%= f.email_field :email, :class =&gt; "target", :placeholder =&gt; "example@gmail.com", :maxlength =&gt; "55" %&gt;
    &lt;%= f.label :password %&gt;
    &lt;%= f.password_field :password, :class =&gt; "target", :placeholder =&gt; "Enter a password", :maxlength =&gt; "55" %&gt;
    &lt;%= f.label :password_confirmation, "Confirmation" %&gt;
    &lt;%= f.password_field :password_confirmation, :class =&gt; "target", :placeholder =&gt; "Enter your password again", :maxlength =&gt; "55" %&gt;
    &lt;%= f.fields_for :preference do |builder| %&gt;
      &lt;%= builder.hidden_field :layout, value: params[:layout] %&gt;
      &lt;%= builder.hidden_field :background_fill, value: params[:background_fill] %&gt;
      &lt;%= builder.hidden_field :background_position, value: params[:background_position] %&gt;
    &lt;% end %&gt;
    &lt;%= f.submit "Create an Account", :class =&gt; "button cta" %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>And my parameters</p>
  
  <pre><code>{"utf8"=&gt;"✓",
  "authenticity_token"=&gt;"[redacted]=",
  "user"=&gt;{"full_name"=&gt;"test",
  "email"=&gt;"test@example.com",
  "password"=&gt;"[FILTERED]",
  "password_confirmation"=&gt;"[FILTERED]",
  "preference"=&gt;{"layout"=&gt;"layout-3",
  "background_fill"=&gt;"restaurant-2",
  "background_position"=&gt;"position-center"}},
  "commit"=&gt;"Create an Account"}
  </code></pre>
  
  <p><strong>Edit:</strong>
  The error I get is <code>Preference(#70124732528700) expected, got ActiveSupport::HashWithIndifferentAccess(#70124687315200)</code>. My understanding is that <code>@user.build_preference</code> and <code>accepts_nested_attributes_for :preference</code> would just work on its own.</p>
  
  <p>Do I need to create a <code>def preferences_attributes=</code> as per?
  <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for" rel="nofollow noreferrer">http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for</a></p>
  
  <p><strong>Update</strong>
  Okay so I think I'm getting a bit closer. Poking around in the rails console, I figured I need to create Preference.new inside the UserController before I can pass in the hash. Since I'm not sure sure what build_preference does exactly, I'm not having any luck yet.</p>
  
  <p>I've tried adding <code>@user.preference = Preference.new</code> above build preference and changing <code>f.field_for :preference</code> to <code>f.field_for @user.preference</code> but I'm still getting the same error.</p>
  
  <p><strong>Update 2</strong>
  For anyone else that's stuck on this problem, the answer is to change <code>f.field_for :preference</code> to <code>f.field_for :preference_attributes</code>. See comment below by <a href="https://stackoverflow.com/users/17410/zetetic">zetetic</a>.</p>