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