# Sessid

**URL:** <https://www.ruby-forum.com/t/sessid/94531>\
**Category:** Rails\
**Created:** [May 5, 2006, 10:20pm UTC](https://www.ruby-forum.com/t/sessid/94531 "2006-05-05T22:20:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![flopezr333](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/54ee81/32.png) [@flopezr333](https://www.ruby-forum.com/u/flopezr333)\
**Post date:** [May 5, 2006, 10:20pm UTC](https://www.ruby-forum.com/t/sessid/94531/1 "2006-05-05T22:20:37Z")

</div>

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 L.

---

<div class="post-metadata">

**Author:** ![flopezr333](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/54ee81/32.png) [@flopezr333](https://www.ruby-forum.com/u/flopezr333)\
**Post date:** [May 5, 2006, 11:29pm UTC](https://www.ruby-forum.com/t/sessid/94531/2 "2006-05-05T23:29:00Z")

</div>

On May 5, 2006, at 1:19 PM, Fernando L. 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

---

<div class="post-metadata">

**Author:** ![flopezr333](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/54ee81/32.png) [@flopezr333](https://www.ruby-forum.com/u/flopezr333)\
**Post date:** [May 8, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/sessid/94531/3 "2006-05-08T19:12:33Z")

</div>

Jeremy K. 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 L.

---

<div class="post-metadata">

**Author:** ![flopezr333](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/54ee81/32.png) [@flopezr333](https://www.ruby-forum.com/u/flopezr333)\
**Post date:** [May 8, 2006, 7:49pm UTC](https://www.ruby-forum.com/t/sessid/94531/4 "2006-05-08T19:49:47Z")

</div>

Jeremy K. 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 L.

---

<div class="post-metadata">

**Author:** ![flopezr333](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/54ee81/32.png) [@flopezr333](https://www.ruby-forum.com/u/flopezr333)\
**Post date:** [May 8, 2006, 8:14pm UTC](https://www.ruby-forum.com/t/sessid/94531/5 "2006-05-08T20:14:22Z")

</div>

On May 8, 2006, at 10:46 AM, Fernando L. 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

---

<div class="post-metadata">

**Author:** ![flopezr333](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/54ee81/32.png) [@flopezr333](https://www.ruby-forum.com/u/flopezr333)\
**Post date:** [May 8, 2006, 7:37pm UTC](https://www.ruby-forum.com/t/sessid/94531/6 "2006-05-08T19:37:35Z")

</div>

On May 8, 2006, at 10:09 AM, Fernando L. 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
