# Getting Session ID

**URL:** <https://www.ruby-forum.com/t/getting-session-id/59171>\
**Category:** Rails\
**Created:** [April 29, 2006, 9:45pm UTC](https://www.ruby-forum.com/t/getting-session-id/59171 "2006-04-29T21:45:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wmerydith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ccd318/32.png) [@wmerydith](https://www.ruby-forum.com/u/wmerydith)\
**Post date:** [April 29, 2006, 9:45pm UTC](https://www.ruby-forum.com/t/getting-session-id/59171/1 "2006-04-29T21:45:34Z")

</div>

I thought I understood how to grab the session id but in the following  
code I am getting null for the session id:

@foo = Foo.new(:session\_id =\> session[:session\_id],  
:test\_id =\> @test.id)

---

<div class="post-metadata">

**Author:** ![wmerydith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ccd318/32.png) [@wmerydith](https://www.ruby-forum.com/u/wmerydith)\
**Post date:** [April 29, 2006, 10:11pm UTC](https://www.ruby-forum.com/t/getting-session-id/59171/2 "2006-04-29T22:11:31Z")

</div>

Will M. wrote:

> I thought I understood how to grab the session id but in the following  
> code I am getting null for the session id:
> 
> @foo = Foo.new(:session\_id =\> session[:session\_id],  
> :test\_id =\> @test.id)

I am closer I believe:

@foo = Foo.new(:session\_id =\> session.id], :test\_id =\> @test.id)

Now the problem is that the session id I am getting is not what I  
expect:

Processing . . . [POST]  
Session ID: d3aa0519df929116ea8e63cb353a1982  
INSERT INTO foo (`test_id`, `session_id`) VALUES(1, 30652084)

The session ids do not match.

---

<div class="post-metadata">

**Author:** ![wmerydith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ccd318/32.png) [@wmerydith](https://www.ruby-forum.com/u/wmerydith)\
**Post date:** [April 30, 2006, 2:57am UTC](https://www.ruby-forum.com/t/getting-session-id/59171/3 "2006-04-30T02:57:40Z")

</div>

Will M. wrote:  
[…]

> I am closer I believe:
> 
> @foo = Foo.new(:session\_id =\> session.id], :test\_id =\> @test.id)
> 
> Now the problem is that the session id I am getting is not what I  
> expect:
> 
> Processing . . . [POST]  
> Session ID: d3aa0519df929116ea8e63cb353a1982  
> INSERT INTO foo (`test_id`, `session_id`) VALUES(1, 30652084)
> 
> The session ids do not match.

The session isn’t an AR instance so you’re getting Ruby’s Object#id  
method which isn’t what you want.

I believe that session is an instance of CGI::Session so you probably  
really want session.session\_id instead.

---

<div class="post-metadata">

**Author:** ![wmerydith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ccd318/32.png) [@wmerydith](https://www.ruby-forum.com/u/wmerydith)\
**Post date:** [May 3, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/getting-session-id/59171/4 "2006-05-03T19:09:41Z")

</div>

Everything else aside… why on earth are you trying to capture the  
session id? Rails is handling the user sessions for you, all you need  
to do is work with the session object in your controllers and views.  
I honestly can’t think of a single reason why I would want to grab a  
session id and then put it into one of my own tables…

-Brian

---

<div class="post-metadata">

**Author:** ![wmerydith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ccd318/32.png) [@wmerydith](https://www.ruby-forum.com/u/wmerydith)\
**Post date:** [May 3, 2006, 7:10pm UTC](https://www.ruby-forum.com/t/getting-session-id/59171/5 "2006-05-03T19:10:35Z")

</div>

On Apr 29, 2006, at 5:57 PM, Mike F. wrote:

> > Processing . . . [POST]  
> > Session ID: d3aa0519df929116ea8e63cb353a1982  
> > INSERT INTO foo (`test_id`, `session_id`) VALUES(1, 30652084)
> > 
> > The session ids do not match.
> 
> The session isn’t an AR instance so you’re getting Ruby’s Object#id  
> method which isn’t what you want.
> 
> I believe that session is an instance of CGI::Session so you probably  
> really want session.session\_id instead.

I’m not sure what the de-facto-standard is here… but I typically  
treat the session object as if it were a magical hash.

session[:session\_id]… just like you’d do with params, cookies, and  
flash.

@foo = Foo.new( :session\_id =\> session[:session\_id], :test\_id =\>  
@test.id )

Cheers,

Robby

Robby R.  
Founder & Executive Director

PLANET ARGON, LLC  
Ruby on Rails Development, Consulting & Hosting

> **[Planet Argon](https://www.planetargon.com)**
>
> Planet Argon is a Ruby on Rails development agency specializing in maintenance and support for existing Rails apps. Based in Portland, OR, Planet Argon has been a member of the Rails community since 2004.

[www.robbyonrails.com](http://www.robbyonrails.com)

+1 503 445 2457  
+1 877 55 ARGON [toll free]  
+1 815 642 4968 [fax]

---

<div class="post-metadata">

**Author:** ![wmerydith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ccd318/32.png) [@wmerydith](https://www.ruby-forum.com/u/wmerydith)\
**Post date:** [May 3, 2006, 7:10pm UTC](https://www.ruby-forum.com/t/getting-session-id/59171/6 "2006-05-03T19:10:35Z")

</div>

On Apr 29, 2006, at 5:57 PM, Mike F. wrote:

> > Processing . . . [POST]  
> > Session ID: d3aa0519df929116ea8e63cb353a1982  
> > INSERT INTO foo (`test_id`, `session_id`) VALUES(1, 30652084)
> > 
> > The session ids do not match.
> 
> The session isn’t an AR instance so you’re getting Ruby’s Object#id  
> method which isn’t what you want.
> 
> I believe that session is an instance of CGI::Session so you probably  
> really want session.session\_id instead.

I’m not sure what the de-facto-standard is here… but I typically  
treat the session object as if it were a magical hash.

session[:session\_id]… just like you’d do with params, cookies, and  
flash.

@foo = Foo.new( :session\_id =\> session[:session\_id], :test\_id =\>  
@test.id )

Cheers,

Robby

Robby R.  
Founder & Executive Director

PLANET ARGON, LLC  
Ruby on Rails Development, Consulting & Hosting

> **[Planet Argon](https://www.planetargon.com)**
>
> Planet Argon is a Ruby on Rails development agency specializing in maintenance and support for existing Rails apps. Based in Portland, OR, Planet Argon has been a member of the Rails community since 2004.

[www.robbyonrails.com](http://www.robbyonrails.com)

+1 503 445 2457  
+1 877 55 ARGON [toll free]  
+1 815 642 4968 [fax]
