Hello all. I have the following code in my Accounts controller: @account = Account.create(params[:account]) @website = @account.websites.create(params[:website]) @contact = @account.contact.create(params[:contact]) My Account model has_one :contact and my Contact belongs_to :account, yet I get the following error: NoMethodError: You have a nil object when you didn''t expect it! The error occurred while evaluating nil.create - - - - - - I''ve scoured the documentation and can''t find where I''m mucking things up. Any help is greatly appreciated! -tb -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
@account = Account.create(params[:account]) now you have new account - contact is empty @account.contact is nil -> calling @account.contact.create is the same as calling nil.create try: @account = Account.create(params[:account]) @account.contact = Contact.create(params[:contact]) On Nov 12, 11:10 pm, "T. B." <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello all. I have the following code in my Accounts controller: > > @account = Account.create(params[:account]) > @website = @account.websites.create(params[:website]) > @contact = @account.contact.create(params[:contact]) > > My Account model has_one :contact and my Contact belongs_to :account, > yet I get the following error: > > NoMethodError: You have a nil object when you didn''t expect it! > The error occurred while evaluating nil.create > > - - - - - - > > I''ve scoured the documentation and can''t find where I''m mucking things > up. Any help is greatly appreciated! > > -tb > -- > Posted viahttp://www.ruby-forum.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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 12, 2008, at 5:29 PM, reHa wrote:> @account = Account.create(params[:account]) > > now you have new account - contact is empty > > @account.contact is nil -> calling @account.contact.create is the same > as calling nil.create > > try: > > @account = Account.create(params[:account]) > @account.contact = Contact.create(params[:contact]) > > On Nov 12, 11:10 pm, "T. B." <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> Hello all. I have the following code in my Accounts controller: >> >> @account = Account.create(params[:account]) >> @website = @account.websites.create(params[:website]) >> @contact = @account.contact.create(params[:contact])@contact = @account.create_contact(params[:contact])>> >> My Account model has_one :contact and my Contact belongs_to :account, >> yet I get the following error: >> >> NoMethodError: You have a nil object when you didn''t expect it! >> The error occurred while evaluating nil.create >> >> - - - - - - >> >> I''ve scoured the documentation and can''t find where I''m mucking >> things >> up. Any help is greatly appreciated! >> >> -tbIt''s right there in the docs for has_one. Compare to the similar semantics for has_many associations that you''re obviously familiar with. http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001385 -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---