Bob Silva
2006-Feb-26 04:36 UTC
[Rails] Creating objects with has_many :through relationship
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/
rgordon@mwt.net
2006-Feb-26 04:43 UTC
[Rails] Creating objects with has_many :through relationship
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> 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/ > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Bob Silva
2006-Feb-26 04:48 UTC
[Rails] Creating objects with has_many :through relationship
Hi Rob,> ac = AdminContact.new > ac.user = user_object > client.admin_contacts << acand> 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/> -----Original Message----- > From: rails-bounces@lists.rubyonrails.org [mailto:rails- > bounces@lists.rubyonrails.org] On Behalf Of rgordon@mwt.net > Sent: Saturday, February 25, 2006 8:43 PM > To: rails@lists.rubyonrails.org > Subject: Re: [Rails] Creating objects with has_many :through relationship > > 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 > > > > 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/ > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
rgordon@mwt.net
2006-Feb-26 06:49 UTC
[Rails] Creating objects with has_many :through relationship
> 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 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''> > 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/ > > >> -----Original Message----- >> From: rails-bounces@lists.rubyonrails.org [mailto:rails- >> bounces@lists.rubyonrails.org] On Behalf Of rgordon@mwt.net >> Sent: Saturday, February 25, 2006 8:43 PM >> To: rails@lists.rubyonrails.org >> Subject: Re: [Rails] Creating objects with has_many :through >> relationship >> >> 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 >> >> >> > 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/ >> > >> > >> > _______________________________________________ >> > Rails mailing list >> > Rails@lists.rubyonrails.org >> > http://lists.rubyonrails.org/mailman/listinfo/rails >> > >> >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Ezra Zygmuntowicz
2006-Feb-26 21:45 UTC
[Rails] Creating objects with has_many :through relationship
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 On Feb 25, 2006, at 8:34 PM, Bob Silva wrote:> 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/ > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >