# Can't create has\_one record

**URL:** <https://www.ruby-forum.com/t/cant-create-has-one-record/153173>\
**Category:** Rails\
**Created:** [November 12, 2008, 11:10pm UTC](https://www.ruby-forum.com/t/cant-create-has-one-record/153173 "2008-11-12T23:10:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vineire77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9c0ed/32.png) [@vineire77](https://www.ruby-forum.com/u/vineire77)\
**Post date:** [November 12, 2008, 11:10pm UTC](https://www.ruby-forum.com/t/cant-create-has-one-record/153173/1 "2008-11-12T23:10:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vineire77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9c0ed/32.png) [@vineire77](https://www.ruby-forum.com/u/vineire77)\
**Post date:** [November 12, 2008, 11:30pm UTC](https://www.ruby-forum.com/t/cant-create-has-one-record/153173/2 "2008-11-12T23:30:13Z")

</div>

@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])

---

<div class="post-metadata">

**Author:** ![vineire77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9c0ed/32.png) [@vineire77](https://www.ruby-forum.com/u/vineire77)\
**Post date:** [November 12, 2008, 11:53pm UTC](https://www.ruby-forum.com/t/cant-create-has-one-record/153173/3 "2008-11-12T23:53:39Z")

</div>

On Nov 12, 2008, at 5:29 PM, reHa wrote:

> @account.contact = Contact.create(params[:contact])
> 
> On Nov 12, 11:10 pm, “T. B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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])

> > things  
> > up. Any help is greatly appreciated!
> > 
> > -tb

It’s right there in the docs for has\_one. Compare to the similar  
semantics for has\_many associations that you’re obviously familiar with.

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001385)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
