# Trouble saving current user

**URL:** <https://www.ruby-forum.com/t/trouble-saving-current-user/145212>\
**Category:** Rails\
**Created:** [August 6, 2008, 9:01pm UTC](https://www.ruby-forum.com/t/trouble-saving-current-user/145212 "2008-08-06T21:01:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gwendydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gwendydd](https://www.ruby-forum.com/u/gwendydd)\
**Post date:** [August 6, 2008, 9:01pm UTC](https://www.ruby-forum.com/t/trouble-saving-current-user/145212/1 "2008-08-06T21:01:17Z")

</div>

I have an application where users can log in and create records. I am  
trying to get Rails to save the id/name of the user who created the  
record. Somehow, the user id isn’t getting stored, and I can’t tell  
why.

I’m rather new to Rails, and I’m cobbling together different tips from  
different tutorials (and advice from friendly people here), and my guess  
is that I’ve just mushed too much stuff together and don’t know how to  
integrate it all gracefully. Or, just as likely, there’s some glaring  
typo in my code.

In any case, here is the relevant part of the controller:

## ---------------------words\_controller.rb------------------- class WordsController \< ApplicationController before\_filter :login\_required, :only =\> [:new, :create, :edit, :update, :delete] … def new @word = Word.new end def create @modword = (Modword.find\_by\_modspell(params[:modword][:modspell]) || Modword.create(params[:modword])) @word = @current\_user.words.build(params[:word]) @word = Word.new(params[:word].merge(:modword\_id =\> @modword.id)) if @word.save redirect\_to(:action =\> :index) else render :action =\> ‘new’ end end … end

Any idea what’s going on here? If you need more information, I’ll be  
happy to supply it!

Thanks so much!

---

<div class="post-metadata">

**Author:** ![gwendydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gwendydd](https://www.ruby-forum.com/u/gwendydd)\
**Post date:** [August 6, 2008, 11:27pm UTC](https://www.ruby-forum.com/t/trouble-saving-current-user/145212/2 "2008-08-06T23:27:04Z")

</div>

Nevermind. I changed to the following code, and now it works:

def create  
@modword = (Modword.find\_by\_modspell(params[:modword][:modspell])  
|| Modword.create(params[:modword]))  
@word = Word.new(params[:word].merge(:modword\_id =\> @modword.id))  
@word.user\_id = @current\_user.id  
if @word.save  
redirect\_to(:action =\> :index)  
else  
render :action =\> ‘new’  
end  
end

---

<div class="post-metadata">

**Author:** ![gwendydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gwendydd](https://www.ruby-forum.com/u/gwendydd)\
**Post date:** [August 8, 2008, 1:45am UTC](https://www.ruby-forum.com/t/trouble-saving-current-user/145212/3 "2008-08-08T01:45:16Z")

</div>

You could also do:

> ```
> @word = Word.new(params[:word].merge(:modword_id =>  
> 
> ```
> 
> @modword.id, :user\_id =\> @current\_user.id))
