# Recording current\_user

**URL:** <https://www.ruby-forum.com/t/recording-current-user/112847>\
**Category:** Rails\
**Created:** [August 23, 2007, 8:53pm UTC](https://www.ruby-forum.com/t/recording-current-user/112847 "2007-08-23T20:53:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![3quid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/f08c70/32.png) [@3quid](https://www.ruby-forum.com/u/3quid)\
**Post date:** [August 23, 2007, 8:53pm UTC](https://www.ruby-forum.com/t/recording-current-user/112847/1 "2007-08-23T20:53:02Z")

</div>

Hi,

Another noob here I am afraid! I have a questuin about the current\_user  
method that comes with AAA.

I have AAA set up and working fine and if i put \<%=  
self.current\_user.login %\> into a view it shows the login name as  
expected on the page.

What I cant work out is how to record this into the database when the  
user completes a form.

My app so far only has two models. “user.rb” and “outlet.rb”… they  
currently both have has\_and\_belongs\_to\_many set up.

When the user fills out the ‘new outlet’ form’ I want the current\_user  
's id to be stored in the column “user\_id”, in the outlets table.

I am assuming I need to add something to my ‘create’ method in the  
controller, but not sure exactly what.

Hope that is enough info. Thanks in advance,

3Quid

---

<div class="post-metadata">

**Author:** ![3quid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/f08c70/32.png) [@3quid](https://www.ruby-forum.com/u/3quid)\
**Post date:** [August 23, 2007, 9:09pm UTC](https://www.ruby-forum.com/t/recording-current-user/112847/2 "2007-08-23T21:09:47Z")

</div>

On 8/23/07, Graham D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What I cant work out is how to record this into the database when the  
> user completes a form.
> 
> My app so far only has two models. “user.rb” and “outlet.rb”… they  
> currently both have has\_and\_belongs\_to\_many set up.
> 
> When the user fills out the ‘new outlet’ form’ I want the current\_user  
> 's id to be stored in the column “user\_id”, in the outlets table.
> 
> I am assuming I need to add something to my ‘create’ method in the  
> controller, but not sure exactly what.

there’s a few ways of doing this:

1. outlet = Outlet.new(params[:outlet])  
outlet.user = current\_user()  
outlet.save!

2. @user = current\_user()  
#returns a new object of the collection type that has been  
instantiated with attributes and linked to this object through the  
join table but has not yet been saved.  
@user.outlets.build(params[:outlet])  
@user.save!

3. @user = current\_user()  
#returns a new object of the collection type that has been  
instantiated with attributes and linked to this object through the  
join table  
@user.outlets.create(params[:outlet])

4. @user = current\_user()  
#adds one or more objects to the collection by creating associations  
in the join table  
@user.outlets \<\< Outlet.new(params[:outlet])

see here for more info:

[http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000645](http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000645)

Adam

---

<div class="post-metadata">

**Author:** ![3quid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/f08c70/32.png) [@3quid](https://www.ruby-forum.com/u/3quid)\
**Post date:** [August 23, 2007, 11:22pm UTC](https://www.ruby-forum.com/t/recording-current-user/112847/3 "2007-08-23T23:22:37Z")

</div>

Hi Adam

Thanks for the reply,

I am afraid I am still not quite gettng this (stupid brain!) …

I tried the suggestions you gave me but all i seem to get is the  
following error

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

Any help would be most appreciated,

Thanks,

3quid

Adam C. wrote:

> On 8/23/07, Graham D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > What I cant work out is how to record this into the database when the  
> > user completes a form.
> > 
> > My app so far only has two models. “user.rb” and “outlet.rb”… they  
> > currently both have has\_and\_belongs\_to\_many set up.
> > 
> > When the user fills out the ‘new outlet’ form’ I want the current\_user  
> > 's id to be stored in the column “user\_id”, in the outlets table.
> > 
> > I am assuming I need to add something to my ‘create’ method in the  
> > controller, but not sure exactly what.
> 
> there’s a few ways of doing this:
> 
> 1. outlet = Outlet.new(params[:outlet])  
> outlet.user = current\_user()  
> outlet.save!
> 
> 2. @user = current\_user()  
> #returns a new object of the collection type that has been  
> instantiated with attributes and linked to this object through the  
> join table but has not yet been saved.  
> @user.outlets.build(params[:outlet])  
> @user.save!
> 
> 3. @user = current\_user()  
> #returns a new object of the collection type that has been  
> instantiated with attributes and linked to this object through the  
> join table  
> @user.outlets.create(params[:outlet])
> 
> 4. @user = current\_user()  
> #adds one or more objects to the collection by creating associations  
> in the join table  
> @user.outlets \<\< Outlet.new(params[:outlet])
> 
> see here for more info:
> 
> [Peak Obsession](http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000645)
> 
> Adam

---

<div class="post-metadata">

**Author:** ![3quid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/f08c70/32.png) [@3quid](https://www.ruby-forum.com/u/3quid)\
**Post date:** [August 23, 2007, 11:44pm UTC](https://www.ruby-forum.com/t/recording-current-user/112847/4 "2007-08-23T23:44:10Z")

</div>

iOn 8/23/07, Graham D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> “You have a nil object when you didn’t expect it!  
> The error occurred while evaluating nil.outlets”
> 
> Any help would be most appreciated,
> 
> Thanks,
> 
> 3quid

If you’re doing something like this:

@user = current\_user()  
@user.outlets.create(params[:outlet])

and you’re getting that error, it means that @user is nil. Make sure  
that current\_user() actually returns a valid user before attempting to  
add outlets to them.

Adam
