# Current\_user in a model observer

**URL:** <https://www.ruby-forum.com/t/current-user-in-a-model-observer/165982>\
**Category:** Rails\
**Created:** [April 20, 2009, 4:43pm UTC](https://www.ruby-forum.com/t/current-user-in-a-model-observer/165982 "2009-04-20T16:43:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [April 20, 2009, 4:43pm UTC](https://www.ruby-forum.com/t/current-user-in-a-model-observer/165982/1 "2009-04-20T16:43:53Z")

</div>

I need to get the current\_user id in my model observers:

Here is the code for current\_user in the lib/authentication.rb file:

module Authentication  
def self.included(controller)  
controller.send :helper\_method, :current\_user, :logged\_in?,  
:has\_role?, :is\_admin?  
end

def current\_user  
@current\_user ||= User.find(session[:user\_id]) if session[:user\_id]  
end  
…  
end

Here is the propsect\_observer.rb file in models directory which needs  
it:

class ProspectObserver \< ActiveRecord::Observer

def after\_update(prospect)  
prospect.comments.create(:content =\> @comment\_text, :admin\_only =\>  
@admin\_only,  
:commenter\_id =\> current\_user.id) if @comment\_text  
end

end

current\_user is not recognized here. I could include the module in the  
observer, but that is bad(?). Any suggestion on a good alternative?

Thanks for your time.

Bharat

---

<div class="post-metadata">

**Author:** ![bruparel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@bruparel](https://www.ruby-forum.com/u/bruparel)\
**Post date:** [April 20, 2009, 5:42pm UTC](https://www.ruby-forum.com/t/current-user-in-a-model-observer/165982/2 "2009-04-20T17:42:20Z")

</div>

Forget it. Bad design. I am not pursuing this path anymore.
