# Writing the user\_id of user when creating an item

**URL:** <https://www.ruby-forum.com/t/writing-the-user-id-of-user-when-creating-an-item/89259>\
**Category:** Rails\
**Created:** [March 19, 2007, 3:03am UTC](https://www.ruby-forum.com/t/writing-the-user-id-of-user-when-creating-an-item/89259 "2007-03-19T03:03:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pleigh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f08c70/32.png) [@pleigh](https://www.ruby-forum.com/u/pleigh)\
**Post date:** [March 19, 2007, 3:03am UTC](https://www.ruby-forum.com/t/writing-the-user-id-of-user-when-creating-an-item/89259/1 "2007-03-19T03:03:57Z")

</div>

hi there, i have this problem, i am a newb in ror and don’t know how to  
do this. i have a user\_id column in a database table named items, i also  
use the login generator, i can signup, login, and logout, no problem  
with it, but when i am logon to the system, and tried to make a new  
item, the user\_id field is null, my user\_id is not written in the  
user\_id table, and i have really no idea how to do it. i have  
associations, has\_many :items in user.rb model, and belongs\_to :user in  
item.rb.

i know you can help me with this, like i’ve said, i am just learning ror  
and new to this forum as well. please post your replies on what info you  
need to help me.

thanks in advance.

---

<div class="post-metadata">

**Author:** ![pleigh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f08c70/32.png) [@pleigh](https://www.ruby-forum.com/u/pleigh)\
**Post date:** [March 19, 2007, 6:21am UTC](https://www.ruby-forum.com/t/writing-the-user-id-of-user-when-creating-an-item/89259/2 "2007-03-19T06:21:57Z")

</div>

You likely want to do something like this is your controller:

current\_user.items.create(params[:item])

This will automatically set the user\_id for you.

–  
Zack C.  
[http://depixelate.com](http://depixelate.com)

---

<div class="post-metadata">

**Author:** ![pleigh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f08c70/32.png) [@pleigh](https://www.ruby-forum.com/u/pleigh)\
**Post date:** [March 20, 2007, 6:33am UTC](https://www.ruby-forum.com/t/writing-the-user-id-of-user-when-creating-an-item/89259/3 "2007-03-20T06:33:48Z")

</div>

Another option would be to store the user\_id in session[:user\_id] during  
login. Then when you create a new object:

unless session[:user\_id].empty?  
@thing = Thing.new(params)  
@thing.user\_id = session[:user\_id]  
@thing.save  
else  
flash[:notice] = “Log in before creating something!”  
redirect\_to :somewhere  
end

---

<div class="post-metadata">

**Author:** ![pleigh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f08c70/32.png) [@pleigh](https://www.ruby-forum.com/u/pleigh)\
**Post date:** [March 20, 2007, 10:43am UTC](https://www.ruby-forum.com/t/writing-the-user-id-of-user-when-creating-an-item/89259/4 "2007-03-20T10:43:30Z")

</div>

Pleigh Ful wrote:

> and new to this forum as well. please post your replies on what info you  
> need to help me.
> 
> thanks in advance.

Try the UserStamp plugin: [http://delynnberry.com/projects/userstamp/](http://delynnberry.com/projects/userstamp/)

HTH

Mike
