Has many through: How to Add record and fill fields in join table
<p>I have these models:</p>

<pre><code>class Item &lt; ActiveRecord::Base
  has_many :users, through: :user_items
  has_many :user_items
end

class User &lt; ActiveRecord::Base
  has_many :items, through: :user_items
  has_many :user_items
end

class UserItem &lt; ActiveRecord::Base
  belongs_to :user
  belongs_to :item
end

create_table "user_items", force: :cascade do |t|
  t.integer  "user_id",
  t.integer  "item_id",
  t.integer  "amount"
end
</code></pre>

<p>I'd like to know the best ways to add an item to a user an also set an amount in the join table.</p>

<p>I was doing something like this:</p>

<pre><code>user = User.first
item = Item.first
UserItems.create(user: user, item: item, amount: 5)
</code></pre>

<p>but I don't like it so much, and sometimes it doesn't work properly, because if I run</p>

<pre><code>user.items   #=&gt; []
</code></pre>

<p>I get empty array, it seems like it doesn't get the new changes in DB.</p>

<p>So, is there an easy way to do that? something intuitive like this would be perfect:</p>

<pre><code>user.user_items &lt;&lt; item, amount: 4
</code></pre>

<hr>

<p>Ok, so I did this way and it worked: </p>

<pre><code>user.user_items.create!(item: item, amount: 5) 
</code></pre>

<p>But This way it didn't: </p>

<pre><code>user.user_items &lt;&lt; item, amount: 4 
</code></pre>

<p>Now it's more elegant. But I still have the problem about the caching request. If I run user.items. it is still empty. Any idea? I tried with inverse_of and nothing. I would like to avoid reload or some methods like this. I would like to do in a transparent way, if it is possible</p>