# Getting :accepts\_nested\_attributes\_for to work with a :belongs\_to relationship

**URL:** <https://www.ruby-forum.com/t/getting-accepts-nested-attributes-for-to-work-with-a-belongs-to-relationship/174173>\
**Category:** Rails\
**Created:** [August 21, 2009, 7:15pm UTC](https://www.ruby-forum.com/t/getting-accepts-nested-attributes-for-to-work-with-a-belongs-to-relationship/174173 "2009-08-21T19:15:46Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Billy\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c5a1d2/32.png) [@Billy\_G](https://www.ruby-forum.com/u/Billy_G)\
**Post date:** [August 21, 2009, 7:15pm UTC](https://www.ruby-forum.com/t/getting-accepts-nested-attributes-for-to-work-with-a-belongs-to-relationship/174173/1 "2009-08-21T19:15:46Z")

</div>

Hi Rails hackers,

I just recently came across the fantastic new  
accepts\_nested\_attributes\_for method off Ryans Scraps, checked out the  
Rails blog announcement of the feature in 2.3, watched the Rails Cast,  
etc.

I’m trying to work it into an app where the signup form is for an  
Account model with a nested Owner object, which is actually a User  
object, hooked in via belongs\_to instead of one of the has\_\*  
associations. I would think this would work using the new features,  
but I can’t seem to get past setting up the new form properly. I  
cannot find any documentation or examples out there that show the  
proper way to do this, so I figured I’d ask, I can’t imagine no one  
else is trying to do this.

Here are the two models involved:

class Account \< ActiveRecord::Base  
# Relationships  
belongs\_to :owner, :class\_name =\> ‘User’, :foreign\_key =\>  
‘owner\_id’  
accepts\_nested\_attributes\_for :owner  
has\_many :users  
end

class User \< ActiveRecord::Base  
belongs\_to :account  
end

I have a suspicion that the fact that a user can have an account via  
the account :has\_many users might be tripping up whatever sets up the  
relationship for owner (user) over the belongs\_to.

This is the view code in new.html.haml:

- form\_for :account, :url =\> account\_path do |account|  
= account.text\_field :name
  - account.fields\_for :owner do |owner|  
= owner.text\_field :name

And this is the controller code for the new action:

class AccountsController \< ApplicationController  
# GET /account/new  
def new  
@account = Account.new  
end  
end

When I try to load /account/new I get the following exception:

NameError in Accounts#new  
Showing app/views/accounts/new.html.haml where line #63 raised:  
@account[owner] is not allowed as an instance variable name

If I try to use the mysterious ‘build’ method, it just bombs out in  
the controller:

class AccountsController \< ApplicationController  
# GET /account/new  
def new  
@account = Account.new  
@account.owner.build  
end  
end

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.build

If I try to set this up using @account.owner\_attributes = {} in the  
controller, or @account.owner = User.new, I’m back to the original  
error, “@account[owner] is not allowed as an instance variable name”.

Does anybody have the new accepts\_nested\_attributes\_for method working  
with a belongs\_to relationship?

Many thanks for any guidance,  
Billy
