title: Rails has_many :through association: save instance into join table
body: <p>In our Rails app, there are 3 models:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :administrations, dependent: :destroy
    has_many :calendars, through: :administrations
  end
  
  class Administration &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :calendar
  end
  
  class Calendar &lt; ActiveRecord::Base
    has_many :administrations, dependent: :destroy
    has_many :users, through: :administrations
  end
  </code></pre>
  
  <p>And here are the corresponding migrations:</p>
  
  <pre><code>class CreateUsers &lt; ActiveRecord::Migration
    def change
      create_table :users do |t|
        t.string :first_name
        t.string :last_name
        t.string :email
  
        t.timestamps null: false
      end
    end
  end
  
  class CreateAdministrations &lt; ActiveRecord::Migration
    def change
      create_table :administrations do |t|
        t.references :user, index: true, foreign_key: true
        t.references :calendar, index: true, foreign_key: true
        t.string :role
  
        t.timestamps null: false
      end
    end
  end
  
  class CreateCalendars &lt; ActiveRecord::Migration
    def change
      create_table :calendars do |t|
        t.string :name
  
        t.timestamps null: false
      end
    end
  end
  </code></pre>
  
  <p>We have created the <code>calendar#create</code> action as follows:</p>
  
  <pre><code>def create
      @calendar = current_user.calendars.build(calendar_params)
      if @calendar.save
        flash[:success] = "Calendar created!"
        redirect_to root_url
      else
        render 'static_pages/home'
      end
    end
  </code></pre>
  
  <p>And the corresponding <code>_calendar_form.html.erb</code> partial:</p>
  
  <pre><code>&lt;%= form_for(@calendar) do |f| %&gt;
    &lt;%= render 'shared/error_messages', object: f.object %&gt;
    &lt;div class="field"&gt;
      &lt;%= f.text_field :name, placeholder: "Your new calendar name" %&gt;
    &lt;/div&gt;
    &lt;%= f.submit "Post", class: "btn btn-primary" %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>This "works" since, when we create a new calendar through the form, it does show up in Rails console, when we type <code>Calendar.all</code>.</p>
  
  <p>However, it seems like no new <code>@administration</code> is being created and the Administration table is not updated, as nothing returns when we type <code>Administration.all</code> in the console.</p>
  
  <p>We thought the Administration table, which is the join table between the User table and the Calendar table, and which respectively contains <code>user_id</code> and <code>calendar_id</code> columns, would be updated automatically when creating a new calendar.</p>
  
  <p>How can we achieve this? Do we need to create a specific <code>administration#create</code> action?</p>
  
  <p>UPDATE: based on the comments and the answers, we implemented the following <code>CalendarsController</code>:</p>
  
  <pre><code>class CalendarsController &lt; ApplicationController
  
    def create
      @calendar = current_user.calendars.build(calendar_params)
      if @calendar.save
        current_user.administrations &lt;&lt; @calendar
        @calendar.administration.role = 'creator'
        flash[:success] = "Calendar created!"
        redirect_to root_url
      else
        render 'static_pages/home'
      end
    end
  
  ...
  </code></pre>
  
  <p>However, this returns the following error:</p>
  
  <pre><code>ActiveRecord::AssociationTypeMismatch in CalendarsController#create
  
  unless record.is_a?(reflection.klass) || record.is_a?(reflection.class_name.constantize)
              message = "#{reflection.class_name}(##{reflection.klass.object_id}) expected, got #{record.class}(##{record.class.object_id})"
              raise ActiveRecord::AssociationTypeMismatch, message
            end
          end
  
  app/controllers/calendars_controller.rb:6:in `create'
  </code></pre>
  
  <p>Are we missing something?</p>