Dec 23, 2007

A Caching Current User Method Call

Quite a while back when I first seen RailsCast Episode #1, I implemented a current_user method exactly as it's presented in the video. The problem with this method is that it assumes that the user_id session variable is always set, obviously this is not always the case.

This snippet addresses the problem

class ApplicationController < ActionController::Base
def current_user
session[:user_id] ? @current_user ||= User.find(session[:user_id]) : nil
end
end

No comments: