DbSession

Simplify the task of storing temporary data in the database.

Getting started

Add DbSession to your Gemfile:

gem 'db_session'

Run the bundle command to install it.

After you install DbSession and add it to your Gemfile, you need to run the generator:

rails generate db_session:install

The generator will install an initializer which describes DbSession configuration options.

Configuration

You can set up the life span of a session in config/initializers/db_session.rb:

config.session_validity = 60 * 60 * 24 * 2

This value represent the time in seconds after which a session expire and can potentially be deleted from the database. The expiration of a session is counted from the time it was last modified.

Usage

You can call this methods inside any controller:

set_db_session(key, value)

This method overwrite the content of the session with the key–value pair.

add_to_db_session(key, value)

This method add the key–value pair to the content of the session.

get_from_db_session(key)

This method return the value stored in the session for the specified key. If no key is specified it returns the whole content of the session.

clear_db_session

This method clear the content of the session.

That’s it folks!