Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

A caching current user method call

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.
   1  
   2  class ApplicationController < ActionController::Base
   3    def current_user
   4      session[:user_id] ? @current_user ||= User.find(session[:user_id]) : nil
   5    end
   6  end
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS