Active Record Session Store

A session store backed by an Active Record class. A default class is provided, but any object duck-typing to an Active Record Session class with text session_idand dataattributes is sufficient.

The default assumes a sessionstables with columns:

+id+ (numeric primary key),
+session_id+ (text, or longtext if your session data exceeds 65K), and
+data+ (text or longtext; careful if your session data exceeds 65KB).

The session_idcolumn should always be indexed for speedy lookups. Session data is marshaled to the datacolumn in Base64 format. If the data you write is larger than the column's size limit, ActionController::SessionOverflowError will be raised.

You may configure the table name, primary key, and data column. For example, at the end of config/application.rb:

ActiveRecord::SessionStore::Session.table_name = 'legacy_session_table'
ActiveRecord::SessionStore::Session.primary_key = 'session_id'
ActiveRecord::SessionStore::Session.data_column_name = 'legacy_session_data'

Note that setting the primary key to the session_idfrees you from having a separate idcolumn if you don't want it. However, you must set session.model.id = session.session_idby hand! A before filter on ApplicationController is a good place.

Since the default class is a simple Active Record, you get timestamps for free if you add created_atand updated_at datetime columns to the sessionstable, making periodic session expiration a snap.

You may provide your own session class implementation, whether a feature-packed Active Record or a bare-metal high-performance SQL store, by setting

ActiveRecord::SessionStore.session_class = MySessionClass

You must implement these methods:

self.find_by_session_id(session_id)
initialize(hash_of_session_id_and_data, options_hash = {})
attr_reader :session_id
attr_accessor :data
save
destroy

The example SqlBypass class is a generic SQL session store. You may use it as a basis for high-performance database-specific stores.

Namespace
Constants
SESSION_RECORD_KEY = 'rack.session.record'
 
ENV_SESSION_OPTIONS_KEY = Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY