How to create a record from the belongs_to association?
<p>I have this association:</p>

<p>user.rb</p>

<pre><code>class User &lt; ActiveRecord::Base
  has_many :todo_lists
  has_one :profile
end
</code></pre>

<p>todo_list.rb</p>

<pre><code>class TodoList &lt; ActiveRecord::Base
  belongs_to :user
end
</code></pre>

<p>profile.rb</p>

<pre><code>class Profile &lt; ActiveRecord::Base
  belongs_to :user
end
</code></pre>

<p>And I'm trying to understand the following behavior:</p>

<pre><code>todo_list = TodoList.create(name: "list 1")

todo_list.create_user(username: "foo")

todo_list.user
#&lt;User id: 1, username: "foo", created_at: "2016-05-01 07:09:05", updated_at: "2016-05-01 07:09:05"&gt;

test_user = todo_list.user

test_user.todo_lists # returns an empty list
=&gt; #&lt;ActiveRecord::Associations::CollectionProxy []&gt;

test_user.todo_lists.create(name: "list 2")

test_user.todo_lists
=&gt; #&lt;ActiveRecord::Associations::CollectionProxy [#&lt;TodoList id: 2, name: "list 2", user_id: 1, created_at: "2016-05-01 07:15:13", updated_at: "2016-05-01 07:15:13"&gt;]&gt;
</code></pre>

<p>Why <code>#create_user</code> adds <code>user</code> to <code>todo_list</code> (<code>todo_list.user</code> returns the <code>user</code>), but does not reflect the association when <code>user.todo_lists</code> is called?</p>

<p>EDITED: </p>

<p>Trying to create a record from the <code>belongs_to</code> side in a <code>one-to-one</code> relationship works when using <code>#create_user!</code>. It still does not hold true when creating a record from the <code>belongs_to</code> in a <code>one-to-many</code> relationship, even using <code>#create_user!</code>.</p>

<pre><code>profile = Profile.create(first_name: "user_one")

profile.create_user!(username: "user_one username")

profile.user
=&gt; #&lt;User id: 6, username: "user_one username", created_at: "2016-05-01 18:22:31", updated_at: "2016-05-01 18:22:31"&gt;

user_one = profile.user
=&gt; #&lt;User id: 6, username: "user_one username", created_at: "2016-05-01 18:22:31", updated_at: "2016-05-01 18:22:31"&gt;

user_one.profile # the relationship was created
=&gt; #&lt;Profile id: 2, first_name: "user_one", user_id: 6, created_at: "2016-05-01 18:22:09", updated_at: "2016-05-01 18:22:31"&gt;

todo_list = TodoList.create(name: "a new list")

todo_list.create_user!(username: "user of a new list")

todo_list.user
=&gt; #&lt;User id: 7, username: "user of a new list", created_at: "2016-05-01 18:26:27", updated_at: "2016-05-01 18:26:27"&gt;

user_of_new_list = todo_list.user
=&gt; #&lt;User id: 7, username: "user of a new list", created_at: "2016-05-01 18:26:27", updated_at: "2016-05-01 18:26:27"&gt;

user_of_new_list.todo_lists #still does not create user from todo_list
=&gt; #&lt;ActiveRecord::Associations::CollectionProxy []&gt;
</code></pre>