# Help with nested creation: what am I doing wrong?

**URL:** <https://www.ruby-forum.com/t/help-with-nested-creation-what-am-i-doing-wrong/115479>\
**Category:** Rails\
**Created:** [September 25, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/help-with-nested-creation-what-am-i-doing-wrong/115479 "2007-09-25T17:51:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zeekar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a8b319/32.png) [@Zeekar](https://www.ruby-forum.com/u/Zeekar)\
**Post date:** [September 25, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/help-with-nested-creation-what-am-i-doing-wrong/115479/1 "2007-09-25T17:51:00Z")

</div>

I’ve got nested scaffolded resource objects as indicated below. All  
the routing and display stuff seems to work properly, but when I  
create a new identity, it shows up in the database with a NULL  
user\_id, even though I’m creating the object via the identities  
attribute of a user object. What am I missing?

Relevant snippets:

config/routes.rb:  
map.resources :users do |users|  
users.resources :identities  
end

models/user.rb:  
class User \< ActiveRecord::Base  
has\_many :identities  
end

models/identity.rb:  
class Identity \< ActiveRecord::Base  
belongs\_to :user  
end

controllers/identities\_controller.rb:  
class IdentitiesController \< ApplicationController  
before\_filter :get\_user

def get\_user  
@user = User.find(params[:user\_id])  
end

def create  
@identity = @user.identities.new(params[:identity])  
respond\_to do |format|  
if @identity.save  
flash[:notice] = ‘Identity was successfully created.’  
format.html { redirect\_to identity\_url(@user, @identity) }  
format.xml { head :created, :location =\>  
identity\_url(@user,  
@identity) }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @identity.errors.to\_xml }  
end  
end  
end

end

---

<div class="post-metadata">

**Author:** ![Zeekar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a8b319/32.png) [@Zeekar](https://www.ruby-forum.com/u/Zeekar)\
**Post date:** [September 26, 2007, 4:58am UTC](https://www.ruby-forum.com/t/help-with-nested-creation-what-am-i-doing-wrong/115479/2 "2007-09-26T04:58:18Z")

</div>

try changing  
@identity = @user.identities.new(params[:identity])

to  
@identity = @user.identities.build(params[:identity])
