Forum: Ruby on Rails Switching to ActiveRecord Session Store

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
82e01981fb1270c0a54c544f9af342c5?d=identicon&s=25 JDevine (Guest)
on 2009-02-19 01:54
(Received via mailing list)
I'm having a problem switching from cookie sessions (the default) to
active record sessions.

I've created the sessions table via the rake task, uncommented the

   config.action_controller.session_store = :active_record_store

in my environment.rb file, cleared out my browsers cookies, restarted
the server (natch), but still the it's using cookie_store instead of
active_record store. Just to be sure, I added puts statements in the
initializer block of each separate store's ruby classes in vendor
rails, and only "cookie_store initialized" come out.

What am I missing here? I've tried commenting out the secret key

  #config.action_controller.session = {
  #  :session_key => '_sm_session',
  #  :secret => 'thisisJohnsSecretCodeItNeedsLotsofCharacters'

but I immediately get server failures because the cookie store needs
them and is still active. I also tried replacing the
config.action_controller.session_store = :active_record_store line
with :

ActionController::Base.session_store = :active_record_store

after the initializer block in environment.rb, but it apparently had
no effect, still the cookie_store turns on.

So, any suggestions or guidance appreciated.

ps. The reason I'm trying to switch is that I'm trying to get Facebook
Connect to work via Facebooker, and apparently it requires the
activerecord session store to be used.

Thanks in advanced,

0d56133042e4814c1b2255ea655f2361?d=identicon&s=25 Jeff Burlysystems (jburly)
on 2009-02-19 05:52
(Received via mailing list)
Hi John,

Hard to tell, but I'm wondering if you forgot to run rake:db:migrate
after running rake db:sessions:create?   The latter creates the
migration file in db/migration/, and the former actually creates the
table in the db.  Check your db to make sure the sessions table does

If you did and the tbl does exist, then it's hard to tell what's going
on with your setup.  If you can't find what the problem is, you could
try the following test to see if you can't get db sessions working:

### rails version?
$ rails --version
Rails 2.2.2

### create a test proj:
$ rails testsess

$ cd testsess/

$ cat config/database.yml
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

### mod environment.rb to use db for sess data:
$ cat config/environment.rb
  config.action_controller.session_store = :active_record_store

### create sess and migrate:
$ rake db:sessions:create

$ rake db:migrate

$ sqlite3 db/development.sqlite3 '.tables'
schema_migrations  sessions

$ sqlite3 db/development.sqlite3 '.schema sessions'
NULL, "session_id" varchar(255) NOT NULL, "data" text, "created_at"
datetime, "updated_at" datetime);
CREATE INDEX "index_sessions_on_session_id" ON
"sessions" ("session_id");
CREATE INDEX "index_sessions_on_updated_at" ON
"sessions" ("updated_at");

$ sqlite3 db/development.sqlite3 'select count(id) from sessions'

### create test controller and index:
$ ./script/generate controller testsess

$ cat app/controllers/testsess_controller.rb
class TestsessController < ApplicationController
  def index
    # test storing something in session ...
    session[:ip] = request.remote_addr if not session[:ip]
    @ip = session[:ip]

$ cat app/views/testsess/index.html.erb
Hi <%= @ip %>.

### start dev env and hit it with browser (or wget or ...):
$ ./script/server -p 3044 -d

$ wget -o /dev/null -O - http://localhost:3044/testsess/

### was sess data stored in db?
$ sqlite3 db/development.sqlite3 'select count(id) from sessions'

### yes:
$ sqlite3 db/development.sqlite3 'select id, updated_at from sessions'
1|2009-02-19 04:23:06

### done.
$ kill `cat tmp/pids/`

This topic is locked and can not be replied to.