Forum: Ruby on Rails Getting Session ID

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.
Will M. (Guest)
on 2006-04-29 23:45
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)
Will M. (Guest)
on 2006-04-30 00:11
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.
Mike F. (Guest)
on 2006-04-30 04:57
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.
Brian H. (Guest)
on 2006-05-03 21:09
(Received via mailing list)
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
Robby R. (Guest)
on 2006-05-03 21:10
(Received via mailing list)
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

www.planetargon.com
www.robbyonrails.com

+1 503 445 2457
+1 877 55 ARGON [toll free]
+1 815 642 4968 [fax]
Robby R. (Guest)
on 2006-05-03 21:10
(Received via mailing list)
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

www.planetargon.com
www.robbyonrails.com

+1 503 445 2457
+1 877 55 ARGON [toll free]
+1 815 642 4968 [fax]
This topic is locked and can not be replied to.