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