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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Af93ba6b6b59f22a8f37e8de5702ef98?d=identicon&s=25 Bob Silva (Guest)
on 2006-02-26 05: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 =>
: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 Silva
http://www.railtie.net/
A3e1e545e1fc4c7703f2edd7d9807dfe?d=identicon&s=25 unknown (Guest)
on 2006-02-26 05:44
(Received via mailing list)
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
Af93ba6b6b59f22a8f37e8de5702ef98?d=identicon&s=25 Bob Silva (Guest)
on 2006-02-26 05:50
(Received via mailing list)
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 Silva
http://www.railtie.net/
A3e1e545e1fc4c7703f2edd7d9807dfe?d=identicon&s=25 unknown (Guest)
on 2006-02-26 07: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

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'
8e44c65ac5b896da534ef2440121c953?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-02-26 22:48
(Received via mailing list)
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
This topic is locked and can not be replied to.