I tried to set the session store to be a ActiveRecordStore. I have made the following changes:
# 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
I opened 2 browsers ( a Firefox and a Chrome ). The very strange thing is that if I start
a rails console, and I write: Session.count, I get back 1.
Afterwards, I login in Firefox, and then I refresh Chrome. Now both browsers appear as logged in. What am I doing wrong? Why doesn’t each browser have it’s own session?
EDIT: It seems that my custom Session class is causing the problems. However, I’m not sure why.
EDIT 2: The problem was here:
def self.find_by_session_id(session_id)
find(:first,"session_id = ?",session_id)
end
and it should have been:
def self.find_by_session_id(session_id)
find(:first,:conditions => ["session_id = ?",session_id])
end
The problem was here:
and it should have been: