I''m a Ruby Newbie (hey that rhymes!) and I suspect I haven''t understood the syntax properly. I''m trying to assign one ActiveRecord object to the field of another and I think this should work: >> org = Organisation.new => #<Organisation:0xb7652dd8 @new_record=true, @attributes={"fax_number"=>nil, "postal_address"=>nil, "name"=>nil, "org_structure_id"=>0, "location_address"=>nil, "tin"=>nil, "phone_number"=>nil, "contact"=>nil, "email_address"=>nil}> >> org.name = "Aces High Inc." => "Aces High Inc." >> person = NaturalPerson.new => #<NaturalPerson:0xb764997c @new_record=true, @attributes={"postal_address"=>nil, "fax_number"=>nil, "name"=>nil, "dob"=>nil, "phone_number"=>nil, "visa"=>nil, "mobile_number"=>nil, "email_address"=>nil, "nationality"=>nil}> >> person.name = "Fred Flinstone" => "Fred Flinstone" >> org.contact << person NoMethodError: 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.<< from (irb):13 My Organisation model looks like this: class Organisation < ActiveRecord::Base belongs_to :org_structure belongs_to :natural_person, {:foreign_key => "contact"} has_many :natural_people, :through => :associates validates_associated :org_structure end So by my understanding that second "belongs_to" call is supposed to enable me to make the assignment that''s failing. It''s 1 o''clock in the morning, maybe this will make sense after a sleep... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''m a Ruby Newbie (hey that rhymes!) and I suspect I haven''t understood the syntax properly. I''m trying to assign one ActiveRecord object to the field of another and I think this should work: >> org = Organisation.new => #<Organisation:0xb7652dd8 @new_record=true, @attributes={"fax_number"=>nil, "postal_address"=>nil, "name"=>nil, "org_structure_id"=>0, "location_address"=>nil, "tin"=>nil, "phone_number"=>nil, "contact"=>nil, "email_address"=>nil}> >> org.name = "Aces High Inc." => "Aces High Inc." >> person = NaturalPerson.new => #<NaturalPerson:0xb764997c @new_record=true, @attributes={"postal_address"=>nil, "fax_number"=>nil, "name"=>nil, "dob"=>nil, "phone_number"=>nil, "visa"=>nil, "mobile_number"=>nil, "email_address"=>nil, "nationality"=>nil}> >> person.name = "Fred Flinstone" => "Fred Flinstone" >> org.contact << person NoMethodError: 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.<< from (irb):13 My Organisation model looks like this: class Organisation < ActiveRecord::Base belongs_to :org_structure belongs_to :natural_person, {:foreign_key => "contact"} has_many :natural_people, :through => :associates validates_associated :org_structure end So by my understanding that second "belongs_to" call is supposed to enable me to make the assignment that''s failing. It''s 1 o''clock in the morning, maybe this will make sense after a sleep... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Well, "org.contact << person" is the same as saying "org.contact.<<(person)", which should make the error message clearer: contact is not defined. I guess you didn''t quite get what all the ActiveRecord meta-functions do to the class. The foreign key for :natural_person may be ''contact'', but that is purely related to the database. Sorry, now that I look at the relationships set up, I''m not sure I follow what is there. I understand what you are trying to do, but I don''t think you know what you''ve got there. Either way, you will want this if all the associations are correct: org.natural_people << person Be sure to spend some more time first with Ruby itself, and then more time at the Rails docs (api.rubyonrails.com). Jason On 12/13/06, Michael Henry <michael.k.henry-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > I''m a Ruby Newbie (hey that rhymes!) and I suspect I haven''t understood > the syntax properly. > I''m trying to assign one ActiveRecord object to the field of another and > I think this should work: > > >> org = Organisation.new > => #<Organisation:0xb7652dd8 @new_record=true, > @attributes={"fax_number"=>nil, "postal_address"=>nil, "name"=>nil, > "org_structure_id"=>0, "location_address"=>nil, "tin"=>nil, > "phone_number"=>nil, "contact"=>nil, "email_address"=>nil}> > >> org.name = "Aces High Inc." > => "Aces High Inc." > >> person = NaturalPerson.new > => #<NaturalPerson:0xb764997c @new_record=true, > @attributes={"postal_address"=>nil, "fax_number"=>nil, "name"=>nil, > "dob"=>nil, "phone_number"=>nil, "visa"=>nil, "mobile_number"=>nil, > "email_address"=>nil, "nationality"=>nil}> > >> person.name = "Fred Flinstone" > => "Fred Flinstone" > >> org.contact << person > NoMethodError: 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.<< > from (irb):13 > > My Organisation model looks like this: > > class Organisation < ActiveRecord::Base > belongs_to :org_structure > belongs_to :natural_person, {:foreign_key => "contact"} > has_many :natural_people, :through => :associates > validates_associated :org_structure > end > > So by my understanding that second "belongs_to" call is supposed to > enable me to make the assignment that''s failing. It''s 1 o''clock in the > morning, maybe this will make sense after a sleep... > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Dec-13 14:36 UTC
Re: Assigning objects...
Hi -- On Thu, 14 Dec 2006, Michael Henry wrote:> I''m a Ruby Newbie (hey that rhymes!)That''s usually called a "nuby" :-)> and I suspect I haven''t understood the syntax properly. > I''m trying to assign one ActiveRecord object to the field of another and > I think this should work: > > >> org = Organisation.new > >> org.name = "Aces High Inc." > >> person = NaturalPerson.new > >> person.name = "Fred Flinstone" > >> org.contact << person > NoMethodError: You have a nil object when you didn''t expect it!The << is only for collections, where you''re adding one to many (like library.books << some_book). Here there''s only one of everything. The method you call on the object, for an association, is always the name of the association itself. You have no association called "contact":> My Organisation model looks like this: > > class Organisation < ActiveRecord::Base > belongs_to :org_structure > belongs_to :natural_person, {:foreign_key => "contact"} > has_many :natural_people, :through => :associates > validates_associated :org_structure > endEither you need to set the value like this: org.natural_person = person or, if you want to use org.contact, you need to change the natural_person association to this: belongs_to :contact, :class_name => "NaturalPerson", :foreign_key => "contact_id" and make any corresponding changes in natural_person.rb David -- Q. What''s a good holiday present for the serious Rails developer? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) aka The Ruby book for Rails developers! Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thank you Jason and David for your replies! David said:>> >>>> org = Organisation.new >>>> org.name = "Aces High Inc." >>>> person = NaturalPerson.new >>>> person.name = "Fred Flinstone" >>>> org.contact << person >>>> >> NoMethodError: You have a nil object when you didn''t expect it! >> > > The << is only for collections, where you''re adding one to many (like > library.books << some_book). Here there''s only one of everything. >Brilliant! That''s a bit of info that I didn''t have.> The method you call on the object, for an association, is always the > name of the association itself. You have no association called > "contact": > > >> My Organisation model looks like this: >> >> class Organisation < ActiveRecord::Base >> belongs_to :org_structure >> belongs_to :natural_person, {:foreign_key => "contact"} >> has_many :natural_people, :through => :associates >> validates_associated :org_structure >> end >>OK, this sentence had to make me re-evaluate my understanding of what was going on! I have defined a field "contact" in my table "organisations" and I was expecting this statement do set up the association: belongs_to :natural_person, :foreign_key => "contact" In my mind I was saying "Right, it''s going to derive the class name from the association, so it will correctly get the NaturalPerson class and I''ve stated that the foreign key is called ''contact'' so I can access that class through the ''contact'' member". OK, so that is wrong!> if you want to use org.contact, you need to change the > natural_person association to this: > > belongs_to :contact, > :class_name => "NaturalPerson", > :foreign_key => "contact_id" > > and make any corresponding changes in natural_person.rb >I do want to use org.contact, so I changed my model as per what you''ve put above: class Organisation < ActiveRecord::Base belongs_to :contact, {:class_name => "NaturalPerson", :foreign_key => "contact"} The thing that hasn''t clicked in my mind yet is that the association and foreign key have the same value - they are redundant (in my particular case). So I thought "Maybe the association name is the same as the field name by convention, but I could actually use any arbitrary name". So I tried this: class Organisation < ActiveRecord::Base belongs_to :foo, {:class_name => "NaturalPerson", :foreign_key => "contact"} So if my presumption is correct, I can update the "contact" field in the database by playing with the "foo" association in the ActiveRecord. >> org = Organisation.new >> org.name = "Wacky Widgets Pty Ltd" >> person = NaturalPerson.new >> person.name = "Barney Rubble" >> org.foo = person >> org.save >> puts org.foo.name Barney Rubble >> puts org.contact.name NoMethodError: undefined method `name'' for 2:Fixnum from (irb):14 By George I think I''ve got it! "Contact" is just a plain old integer, as I defined it in the ActiveRecord::Migration. If I used "belongs_to" with the association name "contact" it will override the integer "contact" and add all the funkiness I''m looking for. Thank you, thank you, thank you! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---