rails current user
March 13th, 2010
6 comments
While playing with next app I thought a bit about so common current_user … so a lot of tutorials and descriptions uses this method in application controller.
After thinking a while I found other way to do this, and for me it looks a bit better, any thoughts on it ?
So get started with User model:
class User < ActiveRecord::Base acts_as_authentic cattr_reader :current end [/sourcecode] Now in application controller add filter to set current user: [sourcecode lang="ruby"] class ApplicationController < ActionController::Base before_filter :set_user private def set_user current_user_session = UserSession.find User.send :class_variable_set, :@@current, current_user_session > current_user_session.record end end
Now you can use whatever you want the same method to access current user:
User.current
Added 2010.03.14 :
Warning please do not use this method it is not thread safe – it is only good for single threaded applications.