# Resyful authenticatio current\_user in model

**URL:** <https://www.ruby-forum.com/t/resyful-authenticatio-current-user-in-model/195068>\
**Category:** Rails\
**Created:** [September 16, 2010, 9:31am UTC](https://www.ruby-forum.com/t/resyful-authenticatio-current-user-in-model/195068 "2010-09-16T09:31:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [September 16, 2010, 9:31am UTC](https://www.ruby-forum.com/t/resyful-authenticatio-current-user-in-model/195068/1 "2010-09-16T09:31:15Z")

</div>

Hi,  
I am using Restful authentication for user authentication.  
I want the current logged in user object in my model property.rb  
Problem is current\_user is not working on model.

How can I fix this I need that?

Please help me out.

Thanks,  
Mike

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [September 16, 2010, 9:56am UTC](https://www.ruby-forum.com/t/resyful-authenticatio-current-user-in-model/195068/2 "2010-09-16T09:56:23Z")

</div>

On 16 Sep 2010, at 09:31, Mike D. wrote:

> I am using Restful authentication for user authentication.  
> I want the current logged in user object in my model property.rb  
> Problem is current\_user is not working on model.
> 
> How can I fix this I need that?

You have two options:

1. Pass the current user from your controller into your model method:  
some\_model.some\_method(current\_user)
2. Use threads to set a class variable on the User model. You can  
either use the userstamp gem/plugin to provide you the functionality  
(User.stamper) or extract the necessary code and roll your own:  
[GitHub - grosser/userstamp: This Rails plugin extends ActiveRecord::Base to add automatic updating of created\_by and updated\_by attributes of your models in much the same way that the ActiveRecord::Timestamp module updates created\_(at/on) and updated\_(at/on) attributes.](http://github.com/grosser/userstamp)

The first method respects the separation of MVC better, the second can  
be more convenient in some cases. It’s up to you to decide the best  
route.

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [September 16, 2010, 4:10pm UTC](https://www.ruby-forum.com/t/resyful-authenticatio-current-user-in-model/195068/3 "2010-09-16T16:10:54Z")

</div>

Mike D. wrote:

> Hi,  
> I am using Restful authentication for user authentication.  
> […]

This isn’t directly related to your issue…but if you don’t mind a bit  
of advice, I highly recommend removing restful\_authentication from your  
project as soon as possible. It relies on unmaintainable generated  
code, and so is needlessly hard to work with. I recommend Authlogic  
instead.

> Thanks,  
> Mike

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [September 16, 2010, 9:05pm UTC](https://www.ruby-forum.com/t/resyful-authenticatio-current-user-in-model/195068/4 "2010-09-16T21:05:31Z")

</div>

The third option is to make virtual attribute on the model and always  
set it when needed in the controller

class Model  
attr\_accessor :current\_user

def some\_method  
if current\_user.admin?  
#foo  
else  
#bar  
end  
end  
end

class FoosController

def index  
m = Model.find(…)  
m.current\_user = current\_user  
m.some\_method()  
end

end

Marnen: What’s so ‘unmaintainable’ in your opinion?

Robert Pankowecki
