Forum: Ruby on Rails Sessid.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
88ade3846f380b12dc27cada47138661?d=identicon&s=25 Fernando Lujan (Guest)
on 2006-05-05 22:20
(Received via mailing list)
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
24d2f8804e6bb4b7ea6bd11e0a586470?d=identicon&s=25 Jeremy Kemper (Guest)
on 2006-05-05 23:29
(Received via mailing list)
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
88ade3846f380b12dc27cada47138661?d=identicon&s=25 Fernando Lujan (Guest)
on 2006-05-08 19:12
(Received via mailing list)
Jeremy Kemper wrote:
>> restore a session finding it using the sessid.
>   session.model
>     # ...
>   end
>
>   CGI::Session::ActiveRecordStore.session_class = MyApp::Session

Where 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
24d2f8804e6bb4b7ea6bd11e0a586470?d=identicon&s=25 Jeremy Kemper (Guest)
on 2006-05-08 19:37
(Received via mailing list)
On May 8, 2006, at 10:09 AM, Fernando Lujan wrote:
> 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
88ade3846f380b12dc27cada47138661?d=identicon&s=25 Fernando Lujan (Guest)
on 2006-05-08 19:49
(Received via mailing list)
Jeremy Kemper wrote:
>>
> 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
24d2f8804e6bb4b7ea6bd11e0a586470?d=identicon&s=25 Jeremy Kemper (Guest)
on 2006-05-08 20:14
(Received via mailing list)
On May 8, 2006, at 10:46 AM, Fernando Lujan wrote:
> 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
This topic is locked and can not be replied to.