[Rails] Sessid.

Jeremy Kemper
2006-05-05T21:28:37Z

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