Archive for March 13th, 2010

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
cattr_reader :current
Now in application controller add filter to set current user:
&#91;sourcecode lang="ruby"&#93;
class ApplicationController < ActionController::Base
before_filter :set_user
def set_user
current_user_session = UserSession.find
User.send :class_variable_set, :@@current, current_user_session > current_user_session.record

Now you can use whatever you want the same method to access current user:


Added 2010.03.14 :
Warning please do not use this method it is not thread safe – it is only good for single threaded applications.

Categories: Development Tags: , ,