Class: CGI::Session::ActiveRecordStore

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_id and data attributes is sufficient.

The default assumes a sessions tables 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_id column should always be indexed for speedy lookups. Session data is marshaled to the data column 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/environment.rb:

  CGI::Session::ActiveRecordStore::Session.table_name = 'legacy_session_table'
  CGI::Session::ActiveRecordStore::Session.primary_key = 'session_id'
  CGI::Session::ActiveRecordStore::Session.data_column_name = 'legacy_session_data'

Note that setting the primary key to the session_id frees you from having a separate id column if you don‘t want it. However, you must set session.model.id = session.session_id by 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_at and updated_at datetime columns to the sessions table, 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

  +CGI::Session::ActiveRecordStore.session_class = MySessionClass+

You must implement these methods:

  self.find_by_session_id(session_id)
  initialize(hash_of_session_id_and_data)
  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.

Child modules and classes

Class CGI::Session::ActiveRecordStore::Session
Class CGI::Session::ActiveRecordStore::SqlBypass

Public Class Methods


new (session, option = nil)

Find or instantiate a session given a CGI::Session.

     # File vendor/rails/actionpack/lib/action_controller/session/active_record_store.rb, line 286
286:       def initialize(session, option = nil)
287:         session_id = session.session_id
288:         unless @session = ActiveRecord::Base.silence { @@session_class.find_by_session_id(session_id) }
289:           unless session.new_session
290:             raise CGI::Session::NoSession, 'uninitialized session'
291:           end
292:           @session = @@session_class.new(:session_id => session_id, :data => {})
293:           # session saving can be lazy again, because of improved component implementation
294:           # therefore next line gets commented out:
295:           # @session.save
296:         end
297:       end

Public Instance Methods


close ()

Save and close the session store.

     # File vendor/rails/actionpack/lib/action_controller/session/active_record_store.rb, line 319
319:       def close
320:         if @session
321:           update
322:           @session = nil
323:         end
324:       end

delete ()

Delete and close the session store.

     # File vendor/rails/actionpack/lib/action_controller/session/active_record_store.rb, line 327
327:       def delete
328:         if @session
329:           ActiveRecord::Base.silence { @session.destroy }
330:           @session = nil
331:         end
332:       end

model ()

Access the underlying session model.

     # File vendor/rails/actionpack/lib/action_controller/session/active_record_store.rb, line 300
300:       def model
301:         @session
302:       end

restore ()

Restore session state. The session model handles unmarshaling.

     # File vendor/rails/actionpack/lib/action_controller/session/active_record_store.rb, line 305
305:       def restore
306:         if @session
307:           @session.data
308:         end
309:       end

update ()

Save session store.

     # File vendor/rails/actionpack/lib/action_controller/session/active_record_store.rb, line 312
312:       def update
313:         if @session
314:           ActiveRecord::Base.silence { @session.save }
315:         end
316:       end

Protected Instance Methods


logger ()

     # File vendor/rails/actionpack/lib/action_controller/session/active_record_store.rb, line 335
335:         def logger
336:           ActionController::Base.logger rescue nil
337:         end