title: Use accepts_nested_attributes_for to create new records or update existing
body: <p>Read the big update for the latest information.</p>
  <p>Hey everyone,</p>
  <p>I've got a many-to-many relationship in a rails app that involves three tables: a user table, an interests table, and a join user_interests table that also has a rating value so a user can rate each of their interests on a 1-10 scale.</p>
  <p>I am basically looking for a way for a new user to create their rating when they sign up and edit them at a future date along with any of their profile information at the same time.</p>
  <p>I tried to follow this question <a href="https://stackoverflow.com/questions/2182428/rails-nested-form-with-has-many-through-how-to-edit-attributes-of-join-model">Rails nested form with has_many :through, how to edit attributes of join model?</a> but the problem I'm having is trying to incorporate a select list into the mix and having multiple interests to rate for the user.</p>
  <p>Model Code:</p>
  <pre><code>user.rb
  has_many :user_interests, :dependent =&gt; :destroy
  has_many :interests, :through =&gt; :user_interests, :foreign_key =&gt; :user_id  
  accepts_nested_attributes_for :user_interests
  
  interest.rb
  has_many :user_interests, :dependent =&gt; :destroy
  has_many :users, :through =&gt; :user_interests, :foreign_key =&gt; :interest_id, :dependent =&gt; :destroy
  
  user_interest.rb
  belongs_to :user
  belongs_to :interest
  </code></pre>
  <p>View Code:</p>
  <pre><code>app/views/user/_form.html.erb
  &lt;%= form_for(@user) do |form| %&gt;
    ... user fields
    &lt;%= form.fields_for :user_interests do |ui_form| %&gt;
      ... loop through ALL interests
      &lt;% Interest.all.each do |interest| %&gt;
        &lt;%= ui_form.select :rating, options_for_select(1..10) %&gt;
        &lt;%= ui_form.hidden_field :interest_id, :value =&gt; interest.id %&gt;
      &lt;% end %&gt;
    &lt;% end %&gt;
  &lt;% end %&gt;
  </code></pre>
  <p>I also included the following in the new/edit actions in my controller <code>@user.interests.build.build_interest</code></p>
  <p>The problem I'm running into is that only one interest rating is being passed in the params hash when I want to have multiple. Also I am getting an exception thrown by rails</p>
  <pre><code>Interest(#2172840620) expected, got Array(#2148226700)
  </code></pre>
  <p>What tiny detail did I miss or get wrong that is causing the problem?</p>
  <p>EDIT:</p>
  <p>I found a way to force this to work but it requires manually editing the HTML in chrome developer tools, the :name attribute of my form elements are being generated as <code>user[user_interests_attributes][rating]</code> but if I change it to <code>user[user_interests_attributes][][rating]</code> it will work when I update a record. However I can't manually specify the :name of a form element that is tied to a form object. So what can I do to show that multiple interest ratings are being passed instead of just one that rails thinks?</p>
  <p>BIG Update:</p>
  <p>I got a semi functional version going with some slight changes:</p>
  <p>View code:</p>
  <pre><code>&lt;% form.fields_for :user_interests do |ui_form| %&gt;
  &lt;p&gt;
      &lt;%= ui_form.select :rating, options_for_select(1..5), :selected =&gt; :rating %&gt;
      &lt;%= ui_form.label :interest_title %&gt;
      &lt;%= ui_form.hidden_field :interest_id %&gt;
  &lt;/p&gt;
  &lt;% end %&gt;
  </code></pre>
  <p>Controller code:</p>
  <pre><code>def new
    @user = User.new
    Interest.all.each { |int| @user.user_interests.build({ :interest_id =&gt; int.id }) }
  end
  
  def edit
    @user = @current_user
    Interest.unrated_by_user_id(@user.id).each { |int| @user.user_interests.build({ :interest_id =&gt; int.id }) }
  end
  </code></pre>
  <p>Now I am able to edit and get my user_interests updated or created if no rating exists, but I get an error that user is empty when I try to create a new user. Also I am unable to access any of the interest attributes in the form to display the interest the user is actually rating. Can anyone help with those caveats?</p>