title: Build vs Create in has many through relationship
body: <p>I wonder if anyone can help me to understand about the difference between build and create in Has Many Through (HMT) relationship?</p>
  
  <p>From my understanding after reading through stackoverflow and google, <strong>create is essentially build + save</strong>. However, it seems like create does more than just build + save. It also somehow save into the join table of the HMT relationship as shown below.</p>
  
  <p>I created 3 models: user, wiki, and collaborator.</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :wikis, through: :collaborators
    has_many :collaborators
  end
  
  class Wiki &lt; ActiveRecord::Base
    has_many :users, through: :collaborators
    has_many :collaborators
  end
  
  class Collaborator &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :wiki
  end
  </code></pre>
  
  <p>Then I tested the build and create behavior in <strong><em>rails console</em></strong>:</p>
  
  <pre class="lang-none prettyprint-override"><code>$rails c
  Loading development environment (Rails 4.0.10)
  &gt; user = User.first  #create instance user
  User Load SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
  =&gt; #&lt;User id: 1, name: "Jayzz55"&gt;
  
  &gt; (user.wikis.build(title:"hello",body:"world")).save  #build and save wiki
  SQL INSERT INTO "wikis" ("body", "created_at", "title", "updated_at") VALUES(?,?,?,?) [["body","world"], ["ccreated_at, Sat, 08 Nov 2014 01:39:36 UTC +00:00], ["title","hello"], ["updated_at",Sat, 08 Nov 2014 01:39:36 UTC +00:00]]
  =&gt; true
  
  &gt;wiki1 = Wiki.first  #create instance wiki called wiki1
  =&gt; #&lt;Wiki id:1, title:"hello",body:"world"&gt;
  
  &gt;user.wikis
  =&gt; #&lt;Wiki id:1, title:"hello",body:"world"&gt;
  
  &gt;user.wikis.count
  =&gt; 0
  
  &gt;wiki1.users
  =&gt; []
  
  &gt;Collaborator.all
  =&gt; []
  
  &gt; user.wikis.create(title:"second title",body:"another one")
  begin transaction
  SQL INSERT INTO "wikis" ("body", "created_at", "title", "updated_at") VALUES (?, ?, ?, ?)[0m  [["body", "another one"], ["created_at", Sat, 08 Nov 2014 01:59:39 UTC +00:00], ["title", "second title"], ["updated_at", Sat, 08 Nov 2014 01:59:39 UTC +00:00]]
  SQL INSERT INTO "collaborators" ("created_at", "updated_at", "user_id", "wiki_id") VALUES (?, ?, ?, ?)  [["created_at", Sat, 08 Nov 2014 01:59:39 UTC +00:00], ["updated_at", Sat, 08 Nov 2014 01:59:39 UTC +00:00], ["user_id", 1], ["wiki_id", 2]]
  =&gt; #&lt;Wiki id:2, title:"second title",body:"another one&gt;
  
  &gt;user.wikis
  =&gt; [#&lt;Wiki id: 1, title:"hello",body:"world"&gt;, #&lt;Wiki id:2, title:"second title",body:"another one&gt;]
  
  &gt;user.wikis.count
  =&gt; 1
  
  &gt;wiki2.users
  =&gt;#&lt;User id: 1, name: "Jayzz55"&gt;
  
  &gt;Collaborator.all
  Collaborator Load SELECT "collaborators".* FROM "collaborators"
  =&gt; #&lt;Collaborator id:`, user_id:1, wiki_id: 2&gt;
  </code></pre>
  
  <p><strong>Case wiki1 : build and then save it</strong>
  The data is not saved into the join table. calling <code>user.wikis</code> does return the object wiki, but running <code>user.wikis.count</code> return 0. Furthermore, running <code>wiki1.users</code> doesn't return the object user. Checking the join table Collaborator returns empty array.</p>
  
  <p><strong>Case wiki2 : create</strong>
  The data is saved into the join table. calling <code>user.wikis</code> does return the object wiki. Running <code>user.wikis.count</code> return 1. Furthermore, running <code>wiki1.users</code> does return the object user. Checking the join table Collaborator, it does show the relationship clearly mapped.</p>
  
  <p>Seems like Create is not simply just build + new. I'm curious about this behavior and hopefully someone can share their knowledge on this.</p>