title: form_for models with specific relations
body: <p>I have<br>
  Models  </p>
  
  <pre><code>class Group &lt; ApplicationRecord
    has_many :group_artists
    has_many :singers, -&gt; { where role: "Singer" }, class_name: "GroupArtist"
    has_many :guitarists, -&gt; { where role: "Guitarist" }, class_name: "GroupArtist"
  end
  
  class GroupArtist &lt; ApplicationRecord
    belongs_to :group
    belongs_to :artist
  end
  
  class Artist &lt; ApplicationRecord
    has_many :group_artists
    has_many :groups, through: :group_artists
  end
  </code></pre>
  
  <p>group_artists table has these columns</p>
  
  <pre><code>class CreateGroupArtists &lt; ActiveRecord::Migration[5.1]
    def change
      create_table :group_artists, id: false do |t|
        t.references :group, foreign_key: true, null: false
        t.references :artist, foreign_key: true, null: false
        t.string :role
        t.string :acting
  
        t.timestamps
      end
    end
  end
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>class GroupsController &lt; ApplicationController
    def new
      @group = Group.new
  
      @singers = @group.singers.build
      @guitarists = @group.guitarists.build
  
      @artists = Artist.all // for a selection
    end
  
    def create
      @group = Group.new(allowed_params)
      @group.save
    end
  
    private
  
    def allowed_params
      params.require(:group).permit(:name, :singers, :guitarists, group_artists_attributes: [:group_id, :artist_id, :role, :acting])
    end
  end
  </code></pre>
  
  <p>views/groups/_form.html.erb</p>
  
  <pre><code>&lt;%= form_for @group do |f| %&gt;
    &lt;%= f.label "Singers" %&gt;
    &lt;%= f.fields_for :singers do |singer| %&gt;
      &lt;%= singer.select(:artist_id, @artists.collect { |a| [a.name, a.id.to_i] }, { include_blank: true }) %&gt;
    &lt;% end %&gt;
    &lt;%= f.label "Guitarists" %&gt;
    &lt;%= f.fields_for :guitarists do |guitarist| %&gt;
      &lt;%= guitarist.select(:artist_id, @artists.collect { |a| [a.name, a.id.to_i] }, { include_blank: true }) %&gt;
    &lt;% end %&gt;
    &lt;%= f.submit "Submit" %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>It creates the group all right, but doesn't create the relation in GroupArtist. 
  I know something's missing in the controller part. I should add something after the ".build" like (role: "Singer") but it doesn't do anything as well. </p>
  
  <p>Ruby -v 2.4.1<br>
  Rails -v 5.1.3</p>