# Initializing list with account creation

**URL:** <https://www.ruby-forum.com/t/initializing-list-with-account-creation/155623>\
**Category:** Rails\
**Created:** [December 14, 2008, 8:54pm UTC](https://www.ruby-forum.com/t/initializing-list-with-account-creation/155623 "2008-12-14T20:54:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nek4life](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e9bcb4/32.png) [@nek4life](https://www.ruby-forum.com/u/nek4life)\
**Post date:** [December 14, 2008, 8:54pm UTC](https://www.ruby-forum.com/t/initializing-list-with-account-creation/155623/1 "2008-12-14T20:54:30Z")

</div>

I have a model for users and a model for lists and a model for the  
items.

I have a one-to-one relationship with the user and the list so only  
one list may be created.

I’m having problems when I try to create the list at the same time I  
create the user so they are related by the foreign key on the list.  
How would I go about doing this.

right now I’m trying

def new  
@user = User.new  
@user.list.build  
end

But I get an error message saying that list is nil. I know the  
problem must be with passing the user\_id to the list model, but I have  
no idea how to do this.

---

<div class="post-metadata">

**Author:** ![nek4life](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e9bcb4/32.png) [@nek4life](https://www.ruby-forum.com/u/nek4life)\
**Post date:** [December 14, 2008, 9:06pm UTC](https://www.ruby-forum.com/t/initializing-list-with-account-creation/155623/2 "2008-12-14T21:06:32Z")

</div>

nek4life wrote:

> I have a model for users and a model for lists and a model for the  
> items.
> 
> I have a one-to-one relationship with the user and the list so only  
> one list may be created.
> 
> I’m having problems when I try to create the list at the same time I  
> create the user so they are related by the foreign key on the list.  
> How would I go about doing this.
> 
> right now I’m trying
> 
> def new  
> @user = User.new  
> @user.list.build  
> end
> 
> But I get an error message saying that list is nil. I know the  
> problem must be with passing the user\_id to the list model, but I have  
> no idea how to do this.

Try @user.build\_list.

If a user could have many lists, you’d use @user.lists.build.

---

<div class="post-metadata">

**Author:** ![nek4life](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e9bcb4/32.png) [@nek4life](https://www.ruby-forum.com/u/nek4life)\
**Post date:** [December 15, 2008, 6:21am UTC](https://www.ruby-forum.com/t/initializing-list-with-account-creation/155623/3 "2008-12-15T06:21:49Z")

</div>

On Dec 14, 3:06 pm, Jeremy Weiskotten \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> Try @user.build\_list.
> 
> ## If a user could have many lists, you’d use @user.lists.build.
> 
> Posted viahttp://www.ruby-forum.com/.

Thanks Jeremy. I ended up using your solution in the create method  
after initializing the object in the new method.

def new  
@user = User.new  
@list = List.new  
end

def create  
@user = User.new(params[:user])  
@user.build\_list  
end
