0
我试图将会话存储设置为ActiveRecordStore。我做了以下更改:同一会话由两个浏览器共享?怎么会这样?
# application.rb
config.action_dispatch.session_store = :active_record_store
# session_store.rb
MyApp::Application.config.session_store :active_record_store
ActiveRecord::SessionStore.session_class = Session
# the session class
class Session < ActiveRecord::SessionStore::Session
belongs_to :user
before_save :ensure_user_is_set
def self.find_by_session_id(session_id)
find(:first,"session_id = ?",session_id)
end
private
def ensure_user_is_set
warden_data = self.data["warden.user.user.key"]
if warden_data
user_id = warden_data[1][0]
self.user = User.find(user_id)
end
end
end
我打开了2个浏览器(Firefox和Chrome)。非常奇怪的是,如果我开始 a rails console
,而我写:Session.count
,我回到1
。
之后,我在Firefox中登录,然后刷新Chrome。现在这两个浏览器都显示为已登录。我在做什么错了?为什么每个浏览器都没有它自己的会话?
编辑︰看来我的自定义会话类正在导致问题。但是,我不知道为什么。
编辑2:这个问题在这里:
def self.find_by_session_id(session_id)
find(:first,"session_id = ?",session_id)
end
,它应该是:
def self.find_by_session_id(session_id)
find(:first,:conditions => ["session_id = ?",session_id])
end