title: Rails 4 ActiveRecord::AssociationTypeMismatch
body: <p>I am trying to add a record to a join table <code>user_logs</code> which joins <code>user_actions</code> and <code>users</code>. However, when I try add an item with the following code I get the error message below. Why am I getting this error? I have included the models and schema below for reference.</p>
  
  <pre><code>2.0.0-p353 :001 &gt; user = User.first
  2.0.0-p353 :002 &gt; action = UserAction.first
  2.0.0-p353 :003 &gt; user.user_logs
  UserLog Load (0.3ms)  SELECT `user_logs`.* FROM `user_logs` WHERE `user_logs`.`user_id` = 1
  =&gt; #&lt;ActiveRecord::Associations::CollectionProxy []&gt; 
  2.0.0-p353 :004 &gt; user.user_logs &lt;&lt; action
  (0.3ms)  BEGIN
  (0.2ms)  ROLLBACK
  ActiveRecord::AssociationTypeMismatch: UserLog(#2156995120) expected, got UserAction(#2177269140)
  </code></pre>
  
  <p><strong>User Model</strong></p>
  
  <pre><code>class User &lt; ActiveRecord::Base
     ...
     has_many :user_logs
     has_many :user_actions, :through =&gt; :user_logs
  end
  </code></pre>
  
  <p><strong>User Actions Model</strong></p>
  
  <pre><code>class UserAction &lt; ActiveRecord::Base
     has_many :user_logs
     has_many :users, :through =&gt; :user_logs
  end
  </code></pre>
  
  <p><strong>User Logs Model</strong></p>
  
  <pre><code>class UserLog &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :user_action
  end
  </code></pre>
  
  <p><strong>Schema</strong></p>
  
  <pre><code>create_table "user_actions", force: true do |t|
    t.string   "action",     limit: 40, default: "", null: false
    t.datetime "created_at"
    t.datetime "updated_at"
  end
  
  add_index "user_actions", ["action"], name: "index_user_actions_on_action", unique: true, using: :btree
  
  create_table "user_logs", force: true do |t|
    t.integer  "user_id"
    t.integer  "user_action_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
  </code></pre>