Forum: Ruby on Rails Creating objects with has_many :through relationship

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Bob S. (Guest)
on 2006-02-26 06:37
(Received via mailing list)
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 =>
:class_name => 'User'

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
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 << :user => user_object )

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

client.contacts << user_object

Is this something that should work?

Bob S.
unknown (Guest)
on 2006-02-26 06:44
(Received via mailing list)
I had the same problem. The way I 'solved' it was:

 ac =
 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.

Bob S. (Guest)
on 2006-02-26 06:50
(Received via mailing list)
Hi Rob,

>  ac =
>  ac.user = user_object
>  client.admin_contacts << ac


> client.admin_contacts << :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.
unknown (Guest)
on 2006-02-26 08:51
(Received via mailing list)
> 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





#{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'
Ezra Z. (Guest)
on 2006-02-26 23:48
(Received via mailing list)

	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.

This topic is locked and can not be replied to.