Creating objects with has_many :through relationship


#1

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/


#2

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


#3

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/


#4

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:inid’

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:inconstruct_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:innew’

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:inendorsees’

#{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:increate’

#{RAILS_ROOT}/app/controllers/claim_admin_controller.rb:32:in `each’

#{RAILS_ROOT}/app/controllers/claim_admin_controller.rb:32:in `create’


#5

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