How can I get the sessid from the current session object? For instance, I log in the system, and want to know which sessid I''m using. I''m storing the session using active_record. Later on, I want to restore a session finding it using the sessid. Thanks in advance. Fernando Lujan
On May 5, 2006, at 1:19 PM, Fernando Lujan wrote:> How can I get the sessid from the current session object?session.session_id (regardless of the session store.)> For instance, I log in the system, and want to know which sessid > I''m using. > > I''m storing the session using active_record. Later on, I want to > restore a session finding it using the sessid.Hmm: this is already done automatically based on cookie or query parameter. Do you need to find an arbitrary session? Also, those using the Active Record session store may set the underlying session model: CGI::Session::ActiveRecordStore.session_class = MySessionClass and access the session''s model instance: session.model session.model.is_a?(MySessionClass) (take a look at the default CGI::Session::ActiveRecordStore::Session in actionpack/lib/action_controller/session/active_record_store.rb) So you may # Subclass the default with some additional behavior. class MyApp::Session < CGI::Session::ActiveRecordStore::Session belongs_to :user # ... end CGI::Session::ActiveRecordStore.session_class = MyApp::Session # before def session_user() @session_user ||= User.find(session[:user_id]) end def login(user) ... session[:user_id] = user.id ... end # after session.user def login(user) ... session.user = user ... end Best, jeremy
Jeremy Kemper wrote:> On May 5, 2006, at 1:19 PM, Fernando Lujan wrote: >> How can I get the sessid from the current session object? > > session.session_id (regardless of the session store.) > > >> For instance, I log in the system, and want to know which sessid I''m >> using. >> >> I''m storing the session using active_record. Later on, I want to >> restore a session finding it using the sessid. > > Hmm: this is already done automatically based on cookie or query > parameter. Do you need to find an arbitrary session? > > > Also, those using the Active Record session store may set the > underlying session model: > CGI::Session::ActiveRecordStore.session_class = MySessionClass > > and access the session''s model instance: > session.model > session.model.is_a?(MySessionClass) > > (take a look at the default CGI::Session::ActiveRecordStore::Session > in actionpack/lib/action_controller/session/active_record_store.rb) > > > So you may > # Subclass the default with some additional behavior. > class MyApp::Session < CGI::Session::ActiveRecordStore::Session > belongs_to :user > # ... > end > > CGI::Session::ActiveRecordStore.session_class = MyApp::SessionWhere can I change this code? I want something like this... In my app. session[:user_id] = "15" session[:group_id] = "18" This will generate a session. I will store it in a database, and later on I want to restore it. Without cookies and so forth. Later, I want something like restoring a session. @id = ID OF THE PREVIOUS CREATED SESSION. @session = Session.find(:first, :conditions=> ["sessid = ?", @id]) @session[:user_id] #returns the user id of the session I know that the session data attribute is marshaled. How can I restore it? Thanks in advance. Fernando Lujan
On May 8, 2006, at 10:09 AM, Fernando Lujan wrote:> I want something like this... > > In my app. > > session[:user_id] = "15" > session[:group_id] = "18" > > This will generate a session. I will store it in a database, and > later on I want to restore it. Without cookies and so forth. > > Later, I want something like restoring a session. > > @id = ID OF THE PREVIOUS CREATED SESSION. > @session = Session.find(:first, :conditions=> ["sessid = ?", @id]) > @session[:user_id] #returns the user id of the session > > I know that the session data attribute is marshaled. How can I > restore it?You don''t need any of these mechanics - session load/save is automatic. Just use session directly and forget about it. When you do session[:user_id] = foo, the session is created if not present, updated with :user_id => foo. When the action is complete, the session is serialized and stored for you. On next page load, session is reconstituted according to the value of cookie OR query parameter _session_id. jeremy
Jeremy Kemper wrote:> On May 8, 2006, at 10:09 AM, Fernando Lujan wrote: >> I want something like this... >> >> In my app. >> >> session[:user_id] = "15" >> session[:group_id] = "18" >> >> This will generate a session. I will store it in a database, and >> later on I want to restore it. Without cookies and so forth. >> >> Later, I want something like restoring a session. >> >> @id = ID OF THE PREVIOUS CREATED SESSION. >> @session = Session.find(:first, :conditions=> ["sessid = ?", @id]) >> @session[:user_id] #returns the user id of the session >> >> I know that the session data attribute is marshaled. How can I >> restore it? > > You don''t need any of these mechanics - session load/save is > automatic. Just use session directly and forget about it. > > When you do session[:user_id] = foo, the session is created if not > present, updated with :user_id => foo. When the action is complete, > the session is serialized and stored for you. On next page load, > session is reconstituted according to the value of cookie OR query > parameter _session_id.Hum, the problem is that I''m not loading the session in a page. I will load it in another app. I''m integrating rails with a old system... Because of that, I need to retrieve the values previously stored in the session. To use in the app. Because I can store it in the database, I thought that will be easy to restore the values. Is it possible? Thanks in advance. Fernando Lujan
On May 8, 2006, at 10:46 AM, Fernando Lujan wrote:> Jeremy Kemper wrote: >> You don''t need any of these mechanics - session load/save is >> automatic. Just use session directly and forget about it. >> >> When you do session[:user_id] = foo, the session is created if not >> present, updated with :user_id => foo. When the action is >> complete, the session is serialized and stored for you. On next >> page load, session is reconstituted according to the value of >> cookie OR query parameter _session_id. > > Hum, the problem is that I''m not loading the session in a page. I > will load it in another app. I''m integrating rails with a old > system... Because of that, I need to retrieve the values previously > stored in the session. To use in the app.Session data is serialized to the database using Marshal.dump so direct access from a non-Ruby app is not too useful. There''s probably a simpler way to exchange data between your apps.. jeremy