A Caching Current User Method Call
Join the DZone community and get the full member experience.Join For Free
A simple method that returns the current user and caches the result to reduce database hits. This assumes you're storing user information in User and that the current user is identified by the session variable user_id.
class ApplicationController < ActionController::Base def current_user session[:user_id] ? @current_user ||= User.find(session[:user_id]) : nil end end
Opinions expressed by DZone contributors are their own.