# Acts\_as\_authenticated current\_user in a model?

**URL:** <https://www.ruby-forum.com/t/acts-as-authenticated-current-user-in-a-model/61618>\
**Category:** Rails\
**Created:** [January 16, 2006, 3:16am UTC](https://www.ruby-forum.com/t/acts-as-authenticated-current-user-in-a-model/61618 "2006-01-16T03:16:05Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [January 16, 2006, 3:16am UTC](https://www.ruby-forum.com/t/acts-as-authenticated-current-user-in-a-model/61618/1 "2006-01-16T03:16:05Z")

</div>

What do I need to do to be able to use an acts\_as\_authenticated  
current\_user in a model?  
Is there some sort of include or require I can do some where that would  
allow this?

* * *

Here is what I am trying to do:

* * *

class Setting \< ActiveRecord::Base

before\_create :created\_by\_user # create only  
before\_save :updated\_by\_user # both, create and update

def created\_by\_user  
self.created\_by\_user\_id = current\_user.id  
self.created\_by\_user\_login = current\_user.login  
end

def updated\_by\_user  
self.updated\_by\_user\_id = current\_user.id  
self.updated\_by\_user\_login = current\_user.login  
end

end

* * *

But it gives me this error:

* * *

undefined local variable or method `current\_user’ for  
#Setting:0x35188f8

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [January 17, 2006, 1:30am UTC](https://www.ruby-forum.com/t/acts-as-authenticated-current-user-in-a-model/61618/2 "2006-01-17T01:30:52Z")

</div>

Anyone know how to accomplish what I am trying to do? I still can not  
figure it out. I know I can just update these fields from the actions in  
my controllers (that is how I have it working for now), but it breaks  
DRY and it is more elegant in the model.

scott wrote:

> What do I need to do to be able to use an acts\_as\_authenticated  
> current\_user in a model?  
> Is there some sort of include or require I can do some where that would  
> allow this?
> 
> * * *
> 
> Here is what I am trying to do:
> 
> * * *
> 
> class Setting \< ActiveRecord::Base
> 
> before\_create :created\_by\_user # create only  
> before\_save :updated\_by\_user # both, create and update
> 
> def created\_by\_user  
> self.created\_by\_user\_id = current\_user.id  
> self.created\_by\_user\_login = current\_user.login  
> end
> 
> def updated\_by\_user  
> self.updated\_by\_user\_id = current\_user.id  
> self.updated\_by\_user\_login = current\_user.login  
> end
> 
> end
> 
> * * *
> 
> But it gives me this error:
> 
> * * *
> 
> undefined local variable or method `current\_user’ for  
> #Setting:0x35188f8

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [January 17, 2006, 1:46am UTC](https://www.ruby-forum.com/t/acts-as-authenticated-current-user-in-a-model/61618/3 "2006-01-17T01:46:00Z")

</div>

I think you would need to include AuthenticatedSystem somehow

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [January 17, 2006, 3:52am UTC](https://www.ruby-forum.com/t/acts-as-authenticated-current-user-in-a-model/61618/4 "2006-01-17T03:52:44Z")

</div>

What errors does it cause if you put it directly in your setting.rb?

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [January 17, 2006, 4:31am UTC](https://www.ruby-forum.com/t/acts-as-authenticated-current-user-in-a-model/61618/5 "2006-01-17T04:31:07Z")

</div>

Paul B. wrote:

> What errors does it cause if you put it directly in your setting.rb?

When I put it inside the class, I get:  
undefined method `helper\_method’ for Setting:Class

When I put it before the class, I get:  
undefined method `helper\_method’ for Object:Class

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [January 17, 2006, 3:12am UTC](https://www.ruby-forum.com/t/acts-as-authenticated-current-user-in-a-model/61618/6 "2006-01-17T03:12:14Z")

</div>

Paul B. wrote:

> I think you would need to include AuthenticatedSystem somehow

Thanks for the reply.

I have tried putting “include AuthenticatedSystem” in various places in:  
setting.rb (model)  
environment.rb  
application.rb  
application\_helper.rb

Doing any of the above causes other errors. Any other possibilities I am  
overlooking, or is leaving it in my controllers my only option?

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [January 17, 2006, 4:43am UTC](https://www.ruby-forum.com/t/acts-as-authenticated-current-user-in-a-model/61618/7 "2006-01-17T04:43:54Z")

</div>

On 1/15/06, scott [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> before\_create :created\_by\_user # create only  
> end
> 
> end
> 
> * * *
> 
> But it gives me this error:
> 
> * * *
> 
> undefined local variable or method `current\_user’ for  
> #Setting:0x35188f8

You can’t do this because models have no concept of the session. How  
could they? These are the same models that could be executed in DB  
migrations or console scripts, for example. One solution, is to use a  
class method in your model. Richard L. has a solution for this:  
[http://livsey.org/2005/07/16/adding\_created\_by\_and\_updated\_by\_to\_rails/](http://livsey.org/2005/07/16/adding_created_by_and_updated_by_to_rails/)

class User \< ActiveRecord::Base  
cattr\_accessor :current\_user  
…  
end

class ApplicationController \< ActionController::Base  
include AuthenticatedSystem  
before\_filter { |c| User.current\_user = c.current\_user }  
end

Now, you can access the current user from your models with  
User.current\_user. In console scripts, simply set the current user  
before performing any operations:

> User.current\_user = User.find(1)  
> @setting.save

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [January 17, 2006, 5:21am UTC](https://www.ruby-forum.com/t/acts-as-authenticated-current-user-in-a-model/61618/8 "2006-01-17T05:21:57Z")

</div>

Thanks Rick,  
That article you linked to looks helpful, I think after I read it more  
carefully, I will learn a lot more about how rails works. 🙂  
I will give what you said a try tomorow. I am still learning how to work  
with MVC in rails. I have a good grasp on VC, I am working on what  
can/can’t be done in models to make my apps cleaner.  
-Scott

Rick O. wrote:

> On 1/15/06, scott [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > before\_create :created\_by\_user # create only  
> > end
> > 
> > end
> > 
> > * * *
> > 
> > But it gives me this error:
> > 
> > * * *
> > 
> > undefined local variable or method `current\_user’ for  
> > #Setting:0x35188f8
> 
> You can’t do this because models have no concept of the session. How  
> could they? These are the same models that could be executed in DB  
> migrations or console scripts, for example. One solution, is to use a  
> class method in your model. Richard L. has a solution for this:  
> [http://livsey.org/2005/07/16/adding\_created\_by\_and\_updated\_by\_to\_rails/](http://livsey.org/2005/07/16/adding_created_by_and_updated_by_to_rails/)
> 
> class User \< ActiveRecord::Base  
> cattr\_accessor :current\_user  
> …  
> end
> 
> class ApplicationController \< ActionController::Base  
> include AuthenticatedSystem  
> before\_filter { |c| User.current\_user = c.current\_user }  
> end
> 
> Now, you can access the current user from your models with  
> User.current\_user. In console scripts, simply set the current user  
> before performing any operations:
> 
> > User.current\_user = User.find(1)  
> > @setting.save
> 
> –  
> rick  
> [http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [January 17, 2006, 9:30pm UTC](https://www.ruby-forum.com/t/acts-as-authenticated-current-user-in-a-model/61618/9 "2006-01-17T21:30:16Z")

</div>

Your advice worked except I was getting an error saying method  
current\_user is protected. I had to edit the authenticated\_system.rb.

> public  
> def current\_user  
> @current\_user ||= session[:user] ? User.find\_by\_id(session[:user]) : nil  
> end  
> protected  
> Is having this method public a security problem?

Rick O. wrote:

> You can’t do this because models have no concept of the session. How  
> could they? These are the same models that could be executed in DB  
> migrations or console scripts, for example. One solution, is to use a  
> class method in your model. Richard L. has a solution for this:  
> [http://livsey.org/2005/07/16/adding\_created\_by\_and\_updated\_by\_to\_rails/](http://livsey.org/2005/07/16/adding_created_by_and_updated_by_to_rails/)
> 
> class User \< ActiveRecord::Base  
> cattr\_accessor :current\_user  
> …  
> end
> 
> class ApplicationController \< ActionController::Base  
> include AuthenticatedSystem  
> before\_filter { |c| User.current\_user = c.current\_user }  
> end
> 
> Now, you can access the current user from your models with  
> User.current\_user. In console scripts, simply set the current user  
> before performing any operations:
> 
> > User.current\_user = User.find(1)  
> > @setting.save
> 
> –  
> rick  
> [http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [January 24, 2007, 3:03am UTC](https://www.ruby-forum.com/t/acts-as-authenticated-current-user-in-a-model/61618/10 "2007-01-24T03:03:55Z")

</div>

Something like this problem might also occur in other cases.

So, for example, you might get "undefined local variable or method  
`current\_user’ " if you’ve not granted all to your project user during  
development.

This may occur inadvertently as you drop test and development database  
that have been corrupted and re-“create” them (as in MySQL) but forget  
the “grant all” step.

In this case, you see the errors when you try to access the application  
through the browser.

Once you do “grant all,” you still see the problem in the browser.

What you need to do after “grant all” is to re-start WEBRick because the  
previous DB connection remains with a user with no privileges remains  
active until you stop the WEBRick rails container … so to speak.

masood mortazavi

scott wrote:

> What do I need to do to be able to use an acts\_as\_authenticated  
> current\_user in a model?  
> Is there some sort of include or require I can do some where that would  
> allow this?
> 
> * * *
> 
> Here is what I am trying to do:
> 
> * * *
> 
> class Setting \< ActiveRecord::Base
> 
> before\_create :created\_by\_user # create only  
> before\_save :updated\_by\_user # both, create and update
> 
> def created\_by\_user  
> self.created\_by\_user\_id = current\_user.id  
> self.created\_by\_user\_login = current\_user.login  
> end
> 
> def updated\_by\_user  
> self.updated\_by\_user\_id = current\_user.id  
> self.updated\_by\_user\_login = current\_user.login  
> end
> 
> end
> 
> * * *
> 
> But it gives me this error:
> 
> * * *
> 
> undefined local variable or method `current\_user’ for  
> #Setting:0x35188f8

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [January 24, 2007, 6:27am UTC](https://www.ruby-forum.com/t/acts-as-authenticated-current-user-in-a-model/61618/11 "2007-01-24T06:27:30Z")

</div>

On Jan 23, 2007, at 9:12 PM, Carl J. wrote:

> [http://delynnberry.com/projects/userstamp/](http://delynnberry.com/projects/userstamp/)

Am I reading correctly that this is just creating a class method on  
User that grabs the current\_user out of the session in the  
ApplicationController before\_filter?

Er, leaving aside the fact that this plugin seemed to return the  
incorrect user when I tried it.

-faisal

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [January 24, 2007, 3:12am UTC](https://www.ruby-forum.com/t/acts-as-authenticated-current-user-in-a-model/61618/12 "2007-01-24T03:12:04Z")

</div>

you could also just use this:

[http://delynnberry.com/projects/userstamp/](http://delynnberry.com/projects/userstamp/)

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [January 24, 2007, 7:49am UTC](https://www.ruby-forum.com/t/acts-as-authenticated-current-user-in-a-model/61618/13 "2007-01-24T07:49:36Z")

</div>

> I freely admit that the current Userstamp plugin has some  
> shortcomings (which is why I’m working on the second iteration–to  
> be release soon), but since the plugin simply accesses the  
> currently logged in user via whatever is in the session, are you  
> sure it was the plugin that returned the incorrect user?

Could be. I’m currently using acts\_as\_authenticated, but with enough  
customizations that anything that normally works with  
acts\_as\_authenticated will likely break anyway.

-faisal

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [January 24, 2007, 7:22am UTC](https://www.ruby-forum.com/t/acts-as-authenticated-current-user-in-a-model/61618/14 "2007-01-24T07:22:34Z")

</div>

> On Jan 23, 2007, at 9:12 PM, Carl J. wrote:
> 
> > [http://delynnberry.com/projects/userstamp/](http://delynnberry.com/projects/userstamp/)
> 
> Am I reading correctly that this is just creating a class method on  
> User that grabs the current\_user out of the session in the  
> ApplicationController before\_filter?

You are mostly correct in your interpretation of what the plugin does,  
except you forgot to mention the automatic saving of the current user  
to the created\_by and updated\_by attributes of your ActiveRecord  
objects.

> Er, leaving aside the fact that this plugin seemed to return the  
> incorrect user when I tried it.

I freely admit that the current Userstamp plugin has some shortcomings  
(which is why I’m working on the second iteration–to be release  
soon), but since the plugin simply accesses the currently logged in  
user via whatever is in the session, are you sure it was the plugin  
that returned the incorrect user?

–  
DeLynn B.  
removed\_email\_address@domain.invalid

> **[DeLynn Berry](https://delynnberry.com)**

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [January 24, 2007, 5:58pm UTC](https://www.ruby-forum.com/t/acts-as-authenticated-current-user-in-a-model/61618/15 "2007-01-24T17:58:49Z")

</div>

I am using a variant of acts\_as\_authenticated, which stores the current  
user in a thread variable, which overcomes the problems that using  
class variable has with concurrency. It has been working fine for me.

Gregg

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [January 24, 2007, 8:20am UTC](https://www.ruby-forum.com/t/acts-as-authenticated-current-user-in-a-model/61618/16 "2007-01-24T08:20:39Z")

</div>

> Could be. I’m currently using acts\_as\_authenticated, but with enough  
> customizations that anything that normally works with  
> acts\_as\_authenticated will likely break anyway.

If you are using acts\_as\_authenticated, you should be able to use the  
current\_user method that’s included with the AuthenticatedSystem  
module in the before filter.

–  
DeLynn B.  
removed\_email\_address@domain.invalid

> **[DeLynn Berry](https://delynnberry.com)**

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [January 24, 2007, 7:04pm UTC](https://www.ruby-forum.com/t/acts-as-authenticated-current-user-in-a-model/61618/17 "2007-01-24T19:04:46Z")

</div>

scott wrote:

> Your advice worked except I was getting an error saying method  
> current\_user is protected. I had to edit the authenticated\_system.rb.
> 
> > public  
> > def current\_user  
> > @current\_user ||= session[:user] ? User.find\_by\_id(session[:user]) : nil  
> > end  
> > protected  
> > Is having this method public a security problem?

Scott,

I too was having this problem, but it bothered me that there was an  
error accessing a protected method inside a class that allowed access to  
other protected methods. It looks like the scope of the before\_filter  
block is not inside the Controller. To solve this problem without  
opening the current\_user scope, do the following:

* * *

class ApplicationController \< ActionController::Base

include AuthenticatedSystem  
before\_filter :login\_from\_cookie # \<-- if you use this  
before\_filter :set\_current\_user

# rest of your ApplicationController body

protected

def set\_current\_user  
User.current\_user = self.current\_user  
end

* * *

Anyone know why the scope is off for the block, and if this is by design  
or a defect?

-Dennis
