# Creating objects with has\_many :through relationship

**URL:** <https://www.ruby-forum.com/t/creating-objects-with-has-many-through-relationship/54189>\
**Category:** Rails\
**Created:** [February 26, 2006, 5:37am UTC](https://www.ruby-forum.com/t/creating-objects-with-has-many-through-relationship/54189 "2006-02-26T05:37:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [February 26, 2006, 5:37am UTC](https://www.ruby-forum.com/t/creating-objects-with-has-many-through-relationship/54189/1 "2006-02-26T05:37:53Z")

</div>

Three tables:

users: id

clients: id

admin\_contacts: user\_id, client\_id

class Client  
has\_many :admin\_contacts  
has\_many :contacts, :through =\> :admin\_contacts, :foreign\_key =\>  
:user\_id,  
:class\_name =\> ‘User’  
end

I added the :contacts for ease of use of grabbing the user objects  
associated with the admin\_contact.

This all works great, I can grab the Users that are admin\_contacts for  
the  
client by doing:

users = client.contacts =\> Array of User objects defined in the  
admin\_contacts table

To add admin\_contacts to a client:

client.admin\_contacts \<\< AdminContact.new( :user =\> user\_object )

I also thought this would work, but doesn’t:

client.contacts \<\< user\_object

Is this something that should work?

Bob S.  
[http://www.railtie.net/](http://www.railtie.net/)

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [February 26, 2006, 5:44am UTC](https://www.ruby-forum.com/t/creating-objects-with-has-many-through-relationship/54189/2 "2006-02-26T05:44:12Z")

</div>

I had the same problem. The way I ‘solved’ it was:

ac = AdminContact.new  
ac.user = user\_object  
client.admin\_contacts \<\< ac

Why _that_ worked and the other does not, I do not  
know but would be willing to hear from someone more  
expert than me.

Rob

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [February 26, 2006, 5:50am UTC](https://www.ruby-forum.com/t/creating-objects-with-has-many-through-relationship/54189/3 "2006-02-26T05:50:38Z")

</div>

Hi Rob,

> ac = AdminContact.new  
> ac.user = user\_object  
> client.admin\_contacts \<\< ac

and

> client.admin\_contacts \<\< AdminContact.new( :user =\> user\_object )

are equivalent.

I would think that :through associations have save capabilities. Anyone  
smarter than us know the answer? Surely Rick or Ezra know!?!?

Bob S.  
[http://www.railtie.net/](http://www.railtie.net/)

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [February 26, 2006, 7:51am UTC](https://www.ruby-forum.com/t/creating-objects-with-has-many-through-relationship/54189/4 "2006-02-26T07:51:25Z")

</div>

> are equivalent.  
> I know, I _understand_ them as equivalent, I just did not  
> experience them as such, neither on my linux both nor  
> on windows.

I came upon this by absolutely gutting the related  
classes and putting back together one line at a time  
including a re-gen of db and scaffolding.

It was only when I made the above change did I get it to work.

Here is stack from my original 2/23 post. Is this similar to  
what you are seeing?

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occured while evaluating nil.[]

RAILS\_ROOT: ./script/…/config/…  
Application Trace | Framework Trace | Full Trace

c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/base.rb:1509:in  
`read_attribute' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1198:in`id’

c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/base.rb:1209:in  
`quoted_id' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations/has_many_association.rb:169:in`construct\_sql’

c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/associations/has\_many\_association.rb:8:in  
`initialize' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations.rb:753:in`new’

c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/associations.rb:753:in  
`endorsees' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations.rb:749:in`endorsees’

#{RAILS\_ROOT}/app/controllers/claim\_admin\_controller.rb:50:in `create’

#{RAILS\_ROOT}/app/controllers/claim\_admin\_controller.rb:47:in `each' #{RAILS_ROOT}/app/controllers/claim_admin_controller.rb:47:in`create’

#{RAILS\_ROOT}/app/controllers/claim\_admin\_controller.rb:32:in `each’

#{RAILS\_ROOT}/app/controllers/claim\_admin\_controller.rb:32:in `create’

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [February 26, 2006, 10:48pm UTC](https://www.ruby-forum.com/t/creating-objects-with-has-many-through-relationship/54189/5 "2006-02-26T22:48:16Z")

</div>

Bob-

```
I ran into a similar problem trying to save objects across

```

the :through relationship name like your :contacts. Its been a little  
while(2-3 weeks?) since I ran into the problem and dug into the  
source of :through. I ended up with the conclusion that it wasn’t in  
there to make that work correctly yet. It might have changed, I  
haven’t had a lot of time recently to troll over the patch list on  
trac. But last time I ran into this it was an equal pain as you are  
having now and so i ended up not using :through relationship names to  
save related objects through like that.

```
It seems like a natural thing to want to do though, I mean save

```

objects using the through set up. Not sure what the best workaround  
is right now.

-Ezra
