title: Same Association Twice, Different Names, Second Uses Wrong Column
body: <p>I'm trying to create two associations that are identical in nature but have two different names because they mean different things. I have a feeling that this is a syntax issue I can't quite get right. I'd rather not rewrite the base level models, but I'm open to suggestions. As far as the modeling goes: Raids have players. Raids have leaders (which are players). I want this players to raids association to exist in the players_raids table and leaders_raids table. The two sets of method calls need to be complementary: players.raids and raids.players, and players.raids_led and raids.leaders. </p>
  
  <p><strong>player.rb:</strong></p>
  
  <pre><code>class Player &lt; ActiveRecord::Base
    has_many :players_raids, class_name: 'PlayersRaids'
    has_many :raids, through: :players_raids
    has_many :leaders_raids, class_name: 'LeadersRaids'
    has_many :raids_led, through: :leaders_raids, source: :leader, foreign_key: 'leader_id'
  end
  </code></pre>
  
  <p><strong>raid.rb:</strong></p>
  
  <pre><code>class Raid &lt; ActiveRecord::Base
    has_many :players_raids, class_name: 'PlayersRaids'
    has_many :players, through: :players_raids
    has_many :leaders_raids, class_name: 'LeadersRaids'
    has_many :leaders, through: :leaders_raids
  end
  </code></pre>
  
  <p><strong>players_raids.rb:</strong></p>
  
  <pre><code>class PlayersRaids &lt; ActiveRecord::Base
    belongs_to :player
    belongs_to :raid
  end
  </code></pre>
  
  <p><strong>leaders_raids.rb:</strong></p>
  
  <pre><code>class LeadersRaids &lt; ActiveRecord::Base
    belongs_to :leader, class_name: 'Player'
    belongs_to :raid
  end
  </code></pre>
  
  <p><strong>create_players_raids.rb</strong></p>
  
  <pre><code>class CreatePlayersRaids &lt; ActiveRecord::Migration
    def change
      create_table :players_raids, id: false do |t|
        t.references :player
        t.references :raid
        t.timestamps
      end
    end
  end
  </code></pre>
  
  <p><strong>creates_leaders_raids.rb</strong></p>
  
  <pre><code>class CreateLeadersRaids &lt; ActiveRecord::Migration
    def change
      create_table :leaders_raids, id: false do |t|
        t.integer :leader_id
        t.references :raid
        t.timestamps
      end
    end
  end
  </code></pre>
  
  <p><strong>raids_controller.rb</strong></p>
  
  <pre><code>class RaidsController &lt; ApplicationController
    def create
      @raid = Raid.new(params[:raid])
      @raid.leaders &lt;&lt; current_player
      @raid.players &lt;&lt; current_player
      if @raid.save
        redirect_to raid_path(@raid), notice: "#{@raid.name} created!"
      else
        render 'new'
      end
    end
  end
  </code></pre>
  
  <p>Some of these sections are areas I've been playing around with, namely the 'source' and 'foreign_key' fields on the 'raids_led' association in the Player model and the name/reference that is currently 't.integer :leader_id' in the LeadersRaids table migration file.</p>
  
  <p>When a new raid is created, the current_player (my ApplicationController version of current_user as my Player model is the more common User model) should be added as both a player and a leader for the newly created raid. When I use raid.players, raid.leaders, and player.raids I get back exactly what I expect. However, when I use player.raids_led I get an error that it's trying to use raids_leaders.player_id instead of raids_leaders.leader_id (ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'leaders_raids.player_id' in 'where clause': SELECT <code>players</code>.* FROM <code>players</code> INNER JOIN <code>leaders_raids</code> ON <code>players</code>.<code>id</code> = <code>leaders_raids</code>.<code>leader_id</code> WHERE <code>leaders_raids</code>.<code>player_id</code> = 1). Can anyone help me resolve where my syntax is lacking?</p>
  
  <p><strong>EDIT:</strong>
  I updated these models and it seems to be functioning as was my original intent. Maybe it was just some sleep and a fresh perspective. Feel free to update or comment if there's a better way to do this.</p>
  
  <p><strong>player.rb:</strong></p>
  
  <pre><code>class Player &lt; ActiveRecord::Base
    has_many :characters
    has_many :players_raids, class_name: 'PlayersRaids'
    has_many :raids, through: :players_raids
    has_many :leaders_raids, class_name: 'LeadersRaids'
    has_many :raids_led, through: :leaders_raids, source: :raid
  end
  </code></pre>
  
  <p><strong>raid.rb:</strong></p>
  
  <pre><code>class Raid &lt; ActiveRecord::Base
    has_many :players_raids, class_name: 'PlayersRaids'
    has_many :players, through: :players_raids
    has_many :leaders_raids, class_name: 'LeadersRaids'
    has_many :leaders, through: :leaders_raids, source: :leader
  end
  </code></pre>
  
  <p><strong>leaders_raids.rb:</strong></p>
  
  <pre><code>class LeadersRaids &lt; ActiveRecord::Base
    belongs_to :leader, class_name: 'Player', foreign_key: 'player_id'
    belongs_to :raid
  end
  </code></pre>
  
  <p><strong>create_leaders_raids.rb:</strong></p>
  
  <pre><code>class CreateLeadersRaids &lt; ActiveRecord::Migration
    def change
      create_table :leaders_raids, id: false do |t|
        t.integer :player_id
        t.references :raid
        t.timestamps
      end
    end
  end
  </code></pre>