Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails shared sessions with activerecord

I'm currently using the default cookies as my single sign on (SSO) but some users are getting strange errors after I push an update. I'm considering moving to active record to store sessions but was wondering how I tell rails that the sessions are in another database?

So if I store sessions via AR in App1DB how can all the other apps know thats where to look for sessions?

like image 837
RobH Avatar asked Dec 18 '08 16:12

RobH


2 Answers

Rails most certainly does support database session storage.

In config/environment.rb, uncomment

# config.action_controller.session_store = :active_record_store

Examining \actionpack-2.2.2\lib\action_controller\session\active_record_store.rb shows that CGI::Session::ActiveRecordStore::Session inherits from ActiveRecord::Base.

So at the end of config/environment.rb, you should be able to say

CGI::Session::ActiveRecordStore::Session.establish_connection(
                              :adapter => "mysql",
                              :host => "otherserver",
                              :username => "session_user",
                              :password => "123ABC",
                              :database => "sessions")

or

CGI::Session::ActiveRecordStore::Session.establish_connection(:sessions)

to use a connect defined in config/database.yml


For example, add to config/database.yml:

 sessions_development:
   adapter: mysql
   host: otherserver
   username: sessions_user
   password: 123ABC
   database: sessions

Add to the end of config/environment.rb

 CGI::Session::ActiveRecordStore::Session.establish_connection("sessions_#{RAILS_ENV}")
like image 147
Samuel Avatar answered Sep 28 '22 01:09

Samuel


In rails 2.3

open config/initializers/session_store.rb

uncomment the line ActionController::Base.session_store = :active_record_store

change the key's value on the line which looks like :key => '_YOUR_APP_NAME_session'

then restart your server.

The result will change your store, and invalidate all old cookies

like image 20
John Jansen Avatar answered Sep 28 '22 00:09

John Jansen