# Create on a has\_many through?

**URL:** <https://www.ruby-forum.com/t/create-on-a-has-many-through/87220>\
**Category:** Rails\
**Created:** [February 28, 2007, 11:28pm UTC](https://www.ruby-forum.com/t/create-on-a-has-many-through/87220 "2007-02-28T23:28:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 28, 2007, 11:28pm UTC](https://www.ruby-forum.com/t/create-on-a-has-many-through/87220/1 "2007-02-28T23:28:36Z")

</div>

Hi, I have two models, User and Item, as well as a join model,  
UserItem. So the User model includes the lines:

has\_many :user\_items  
has\_many :items, :through =\> :user\_items

I’ve noticed that this doesn’t work:

u = User.new  
u.items.create :name =\> “hammer”

Any reason why? Seems like it would be a nice feature. To get around  
it, I usually do this:

u.items \<\< Item.create :name =\> “hammer”  
or  
u.user\_items.create(:item =\> User.create(:name =\> “hammer”))

Thanks for your time,

Andrew

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 1, 2007, 12:30am UTC](https://www.ruby-forum.com/t/create-on-a-has-many-through/87220/2 "2007-03-01T00:30:48Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Hi, I have two models, User and Item, as well as a join model,  
> UserItem. So the User model includes the lines:
> 
> has\_many :user\_items  
> has\_many :items, :through =\> :user\_items
> 
> I’ve noticed that this doesn’t work:
> 
> u = User.new  
> u.items.create :name =\> “hammer”
> 
> Any reason why? Seems like it would be a nice feature. To get around  
> it, I usually do this:
> 
> u.items \<\< Item.create :name =\> “hammer”  
> or  
> u.user\_items.create(:item =\> User.create(:name =\> “hammer”))

You can’t do that with create, but create! will do what you want.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 1, 2007, 3:06pm UTC](https://www.ruby-forum.com/t/create-on-a-has-many-through/87220/3 "2007-03-01T15:06:37Z")

</div>

The reason this does not work is because your user object does not  
exist in the database yet and there is no id to create the  
association. either create or save the user first.

see the docs on why

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000642)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.
