Rails 与 activerecord 共享会话 [英] Rails shared sessions with activerecord
问题描述
我目前使用默认 cookie 作为我的单点登录 (SSO),但一些用户在我推送更新后遇到奇怪的错误.我正在考虑使用活动记录来存储会话,但想知道如何告诉 Rails 会话在另一个数据库中?
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?
因此,如果我通过 AR 在 App1DB 中存储会话,所有其他应用程序如何知道在何处查找会话?
So if I store sessions via AR in App1DB how can all the other apps know thats where to look for sessions?
推荐答案
Rails 肯定确实支持数据库会话存储.
Rails most certainly does support database session storage.
在 config/environment.rb 中,取消注释
In config/environment.rb, uncomment
# config.action_controller.session_store = :active_record_store
检查 actionpack-2.2.2libaction_controllersessionactive_record_store.rb 表明 CGI::Session::ActiveRecordStore::Session 继承自 ActiveRecord::Base.
Examining actionpack-2.2.2libaction_controllersessionactive_record_store.rb shows that CGI::Session::ActiveRecordStore::Session inherits from ActiveRecord::Base.
所以在 config/environment.rb 的最后,你应该可以说
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")
或
CGI::Session::ActiveRecordStore::Session.establish_connection(:sessions)
使用在 config/database.yml 中定义的连接
to use a connect defined in config/database.yml
例如添加到config/database.yml:
For example, add to config/database.yml:
sessions_development:
adapter: mysql
host: otherserver
username: sessions_user
password: 123ABC
database: sessions
添加到 config/environment.rb 的末尾
Add to the end of config/environment.rb
CGI::Session::ActiveRecordStore::Session.establish_connection("sessions_#{RAILS_ENV}")
这篇关于Rails 与 activerecord 共享会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!