Forum: Ruby on Rails current_user in a model observer

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bharat R. (Guest)
on 2009-04-20 18:43
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
Bharat R. (Guest)
on 2009-04-20 19:42
Forget it.  Bad design.  I am not pursuing this path anymore.
This topic is locked and can not be replied to.