title: rails has_many through create multiple join records with collection_select
body: <p>I'm pretty new to rails and trying to get three models to work together with a has_many through relationship, alternate class names on the join model, and a collection_select form to add records to the join model. The three models are below. I have "accepts_nested_attributes_for" for all associations.</p>
  
  <p>User Model</p>
  
  <pre><code>has_many :match_opponents
  has_many :home_opponents, :class_name =&gt; 'MatchOpponent', :foreign_key =&gt; 'home_opponent_id'
  has_many :away_opponents, :class_name =&gt; 'MatchOpponent', :foreign_key =&gt; 'away_opponent_id'
  has_many :matches, :through =&gt; :match_opponents
  </code></pre>
  
  <p>Match Model</p>
  
  <pre><code>has_many :match_opponents
  has_many :home_opponents, :class_name =&gt; 'MatchOpponent', :foreign_key =&gt; 'home_opponent_id'
  has_many :away_opponents, :class_name =&gt; 'MatchOpponent', :foreign_key =&gt; 'away_opponent_id'
  has_many :users, :through =&gt; :match_opponents
  </code></pre>
  
  <p>Match Opponent Model</p>
  
  <pre><code>belongs_to :user, :inverse_of =&gt; :match_opponents
  belongs_to :match, :inverse_of =&gt; :match_opponents
  </code></pre>
  
  <p>My Matches Controller for new and create: </p>
  
  <pre><code>def new
    @match = Match.new
    @match.home_opponents.build
    @match.away_opponents.build
  end
  
  def create
    @match = Match.create(match_params)
    @home_opponents = @match.home_opponents.create!(params[:match_opponents])
    @away_opponents = @match.away_opponents.create!(params[:match_opponents])
    if @match.save 
      redirect_to @match, notice: 'Match was successfully created.'
    end
  end
  </code></pre>
  
  <p>My form: </p>
  
  <pre><code>= simple_form_for @match, html: { multipart: true } do |f|
    = simple_fields_for :home_opponents do |ff|
      = ff.collection_select :user_id, User.all, :id, :name, {}, {multiple: true}
  
    = simple_fields_for :away_opponents do |ff|
      = ff.collection_select :user_id, User.all, :id, :name, {}, {multiple: true}
  </code></pre>
  
  <p>Relevant permitted parameters for each model: </p>
  
  <pre><code>def match_params
    params.require(:match).permit(Match::MATCH_ATTRIBUTES)
  end
  
  MATCH_ATTRIBUTES = [:match_opponent_ids =&gt; [], :user_ids =&gt; [], match_opponents_attributes: MatchOpponent::MATCH_OPPONENT_ATTRIBUTES]
  
  def user_params
    params.require(:user).permit(User::USER_ATTRIBUTES)
  end
  
  USER_ATTRIBUTES = [:match_opponent_ids =&gt; [], :match_ids =&gt; [], :match_opponents_attributes: MatchOpponent::MATCH_OPPONENT_ATTRIBUTES]
  
  def match_opponent_params
    params.require(:match_opponent).permit(MatchOpponent::MATCH_OPPONENT_ATTRIBUTES)
  end
  
  MATCH_OPPONENT_ATTRIBUTES = [:id, :home_opponent_id, :away_opponent_id, :match_id, :user_id]
  </code></pre>
  
  <p>I've tried a lot of different configurations of the models but can't get multiple records on the join model (MatchOpponent) to save with both match_id and user_id recorded.</p>
  
  <p>Thanks in advance for any help!</p>