title: incorrect database records created for rails 3 has_many :through association
body: <p>I have a has_many :through association. Players have many Teams and Teams have many Players. The join model, Affiliation, belongs to Players and Teams, and also has a <code>year</code> attribute to keep track of a player's team affiliation (or employment) from year to year.</p>
  <p>I can't seem to figure out the right way to build an association based on the following rules:</p>
  <ol>
  <li>Create a new player.</li>
  <li>Associate a team that may be new or existing. So find it or create it, but only create it if the player is saved.</li>
  <li>The association may or may not include a year, but the association should only be created if the player and team are saved.</li>
  </ol>
  <p>The Player model looks like:</p>
  <pre><code>class Player &lt; ActiveRecord::Base
    attr_accessible :name
    
    has_many :affiliations, :dependent =&gt; :destroy
    has_many :teams, :through =&gt; :affiliations
  end
  </code></pre>
  <p>The Team model looks like:</p>
  <pre><code>class Team &lt; ActiveRecord::Base
    attr_accessible :city
    
    has_many :affiliations, :dependent =&gt; :destroy
    has_many :players, :through =&gt; :affiliations
  end
  </code></pre>
  <p>The Affiliation model looks like:</p>
  <pre><code>class Affiliation &lt; ActiveRecord::Base
    attr_accessible :player_id, :team_id, :year
    belongs_to :player
    belongs_to :team
  end
  </code></pre>
  <p>I have been successful at creating the association records without the join model attribute using a create action in the PlayersController that looks like:</p>
  <pre><code>class PlayersController &lt; ApplicationController
    def create
      @player = Player.new(params[:player].except(:teams))
      
      unless params[:player][:teams].blank?
        params[:player][:teams].each do |team|
          team_to_associate = Team.find_or_initialize_by_id(team[:id], team.except(:year)
          @player.teams &lt;&lt; team_to_associate
        end
      end
      
      @player.save
      respond_with @player
    end
  end
  </code></pre>
  <p>After creating a new player with two teams using params like:</p>
  <pre><code>{&quot;player&quot;=&gt;{&quot;name&quot;=&gt;&quot;George Baker&quot;, &quot;teams&quot;=&gt;[{&quot;city&quot;=&gt;&quot;Buffalo&quot;}, {&quot;city&quot;=&gt;&quot;Detroit&quot;}]}}
  </code></pre>
  <p>the database looks like:</p>
  <p>players</p>
  <blockquote>
  <p>id: 1, name: George Baker</p>
  </blockquote>
  <p>teams</p>
  <blockquote>
  <p>id: 1, city: Buffalo</p>
  <p>id: 2, city: Seattle</p>
  </blockquote>
  <p>affiliations</p>
  <blockquote>
  <p>id: 1, player_id: 1, team_id: 1, year: null</p>
  <p>id: 2, player_id: 1, team_id: 2, year: null</p>
  </blockquote>
  <p>When I try to introduce the year, things fall apart. My most recent attempt at the create action in the PlayersController looks like:</p>
  <pre><code>class PlayersController &lt; ApplicationController
    def create
      @player = Player.new(params[:player].except(:teams))
      
      unless params[:player][:teams].blank?
        params[:player][:teams].each do |team|
          team_to_associate = Team.find_or_initialize_by_id(team[:id], team.except(:year)
          // only additional line...
          team_to_associate.affiliations.build({:year =&gt; team[:year]})
          @player.teams &lt;&lt; team_to_associate
        end
      end
      
      @player.save
      respond_with @player
    end
  end
  </code></pre>
  <p>Now, when creating a new player with two teams using params like:</p>
  <pre><code>{&quot;player&quot;=&gt;{&quot;name&quot;=&gt;&quot;Bill Johnson&quot;, &quot;teams&quot;=&gt;[{&quot;id&quot;=&gt;&quot;1&quot;}, {&quot;city&quot;=&gt;&quot;Detroit&quot;, &quot;year&quot;=&gt;&quot;1999&quot;}]}}
  </code></pre>
  <p>the database looks like:</p>
  <p>players</p>
  <blockquote>
  <p>id: 1, name: George Baker</p>
  <p>id: 2, name: Bill Johnson</p>
  </blockquote>
  <p>teams</p>
  <blockquote>
  <p>id: 1, city: Buffalo</p>
  <p>id: 2, city: Seattle</p>
  <p>id: 3, city: Detroit</p>
  </blockquote>
  <p>affiliations</p>
  <blockquote>
  <p>id: 1, player_id: 1, team_id: 1, year: null</p>
  <p>id: 2, player_id: 1, team_id: 2, year: null</p>
  <p>id: 3, player_id: 2, team_id: 1, year: null</p>
  <p>id: 4, player_id: null, team_id: 3, year: 1999</p>
  <p>id: 5, player_id: 2, team_id: 3, year: null</p>
  </blockquote>
  <p>So three records were created when only two should have been. The affiliation record id: 3 is correct. For id: 4, the player_id is missing. And for id: 5, the year is missing.</p>
  <p>Obviously this is incorrect. Where am I going wrong?</p>
  <p>Thanks</p>