# How can I change the value of the session key

**URL:** <https://www.ruby-forum.com/t/how-can-i-change-the-value-of-the-session-key/85088>\
**Category:** Rails\
**Created:** [February 9, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/how-can-i-change-the-value-of-the-session-key/85088 "2007-02-09T23:11:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stepheneb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@stepheneb](https://www.ruby-forum.com/u/stepheneb)\
**Post date:** [February 9, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/how-can-i-change-the-value-of-the-session-key/85088/1 "2007-02-09T23:11:56Z")

</div>

How can I change the session key “\_session\_id” returned to the client  
from my rails apps. I’m using both 1.2.2 and 1.1.6 frameworks.

I have a strange problem. I have two rails apps that use REST to  
communicate. The problematic interaction occurs when app1 uses a  
redirect to point the client to app2. Both apps appear use the same  
session key identifier: “\_session\_id”. When the re-direct occurs the  
session cookie from app1 is replaced by the session cookie from app  
two because they both use the same key.

Here’s some documentation on changing the primary key using in the  
session database table but I haven’t been able to get it to work:

[http://caboo.se/doc/classes/CGI/Session/ActiveRecordStore.html](http://caboo.se/doc/classes/CGI/Session/ActiveRecordStore.html)

The fifth paragraph suggests:

Note that setting the primary key to the session\_id frees you from  
having a separate id column if you don‘t want it.  
However, you must set session.model.id = session.session\_id by  
hand! A before\_filter on ApplicationController is a  
good place.I though perhaps rails was by default deriving the  
string for the session key from the primary key for the  
session table and tried changing it here and in the migrations but  
even though I’ve changed it every where I have found:

```
CGI::Session::ActiveRecordStore::Session.table_name = "#

```

{RAILS\_APPLICATION\_PREFIX}diy\_sessions"  
CGI::Session::ActiveRecordStore::Session.primary\_key = “#  
{RAILS\_APPLICATION\_PREFIX}diy\_session\_id”

I still get this suspicious error:

Mysql::Error: Unknown column ‘session\_id’ in ‘where clause’: SELECT \*  
FROM teemss2\_diy\_sessions WHERE (session\_id =  
‘8e4863f1ca1943753def464382ce88bb’) LIMIT 1

Sure does look like there is some other place the primary key is set.

Here’s the forensics:

My logged-in user’s been working in app1 and their browser’s been  
happily exchanging their session cookie back and forth with app1:

[http://concord.org/app1/page/list](http://concord.org/app1/page/list)

GET /app1/page/list HTTP/1.1  
Cookie: \_session\_id=da43fd21e94f6096670716f0a9f71549

HTTP/1.x 200 OK  
Set-Cookie: \_session\_id=da43fd21e94f6096670716f0a9f71549; path=/

* * *

Now they request a resource from app1 which re-directs them to app2  
where the jnlp they want is actually generated:

[http://concord.org/app1/page/sail\_jnlp/6/1](http://concord.org/app1/page/sail_jnlp/6/1)

GET /app1/page/sail\_jnlp/6/1 HTTP/1.1  
Cookie: \_session\_id=da43fd21e94f6096670716f0a9f71549

HTTP/1.x 302 Found  
Set-Cookie: \_session\_id=da43fd21e94f6096670716f0a9f71549; path=/

* * *

App1 is the ‘portal’ they login to and interact with but app2  
generates the jnlp and other resources. The response from app2  
however sets a new session cookie with the same session key.

[http://concord.org/app2/offering/139/jnlp/529](http://concord.org/app2/offering/139/jnlp/529)?  
sailotrunk.otmlurl=[http://concord.org/app1/page/otml/6/1](http://concord.org/app1/page/otml/6/1)

GET /sds/5/offering/139/jnlp/529?sailotrunk.otmlurl=http://  
[concord.org/app1/page/otml/6/1](http://concord.org/app1/page/otml/6/1) HTTP/1.1  
Cookie: \_session\_id=da43fd21e94f6096670716f0a9f71549

HTTP/1.x 200 OK  
Set-Cookie: \_session\_id=6df71f8e551b3b2de83d4fb0f43d7d56; path=/

When the Java program starts up it requests a REST resource to  
specify the content from app1 with the sailotrunk.otmlurl url parameter:

[http://concord.org/app1/page/otml/6/1](http://concord.org/app1/page/otml/6/1)

* * *

So after the webstart program has started and the user gets back to  
the browser window with app1 sitting in it and clicks on the home  
page link the browser sends both session cookies back to the server  
but app1 now responds with an entirely new cookie and my logged-in  
user is now not logged-in.

[http://concord.org/app1/home](http://concord.org/app1/home)

GET /app1/home HTTP/1.1  
Cookie: \_session\_id=6df71f8e551b3b2de83d4fb0f43d7d56  
\_session\_id=da43fd21e94f6096670716f0a9f71549

HTTP/1.x 200 OK  
Set-Cookie: \_session\_id=99b095b227bdd6c3918841df3a6c5278; path=/

---

<div class="post-metadata">

**Author:** ![stepheneb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@stepheneb](https://www.ruby-forum.com/u/stepheneb)\
**Post date:** [February 9, 2007, 11:32pm UTC](https://www.ruby-forum.com/t/how-can-i-change-the-value-of-the-session-key/85088/2 "2007-02-09T23:32:57Z")

</div>

I want to customize map.resources so it uses a uid in the URL instead  
of the database id. The uid is a field on my object.

So I have map.resources :things in routes.rb and I can update a thing  
by accessing POST /thing/1. Instead I want to update the thing by  
POST /thing/80ed14ce098affc2 The thing has an id of 1 and a uid of  
80ed14ce098affc2

Any options other then getting rid of map.resources and explicitly  
defining all the REST routes?

Thanks  
Andy

---

<div class="post-metadata">

**Author:** ![stepheneb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@stepheneb](https://www.ruby-forum.com/u/stepheneb)\
**Post date:** [February 10, 2007, 12:06am UTC](https://www.ruby-forum.com/t/how-can-i-change-the-value-of-the-session-key/85088/3 "2007-02-10T00:06:01Z")

</div>

Stephen B. wrote the following on 09.02.2007 23:10 :

> Here’s some documentation on changing the primary key using in the  
> session database table but I haven’t been able to get it to work:
> 
> [http://caboo.se/doc/classes/CGI/Session/ActiveRecordStore.html](http://caboo.se/doc/classes/CGI/Session/ActiveRecordStore.html)

I don’t think you want to change the column used to store the cookie.  
You don’t want your applications cookies to overlap. You can do it  
either by using different system names (accessing your applications  
through [http://app](http://app).concord.org instead of [http://concord.org/app](http://concord.org/app))  
or by using the path attribute of the cookie header.

> […]
> 
> GET /app1/page/list HTTP/1.1  
> Cookie: \_session\_id=da43fd21e94f6096670716f0a9f71549
> 
> HTTP/1.x 200 OK  
> Set-Cookie: \_session\_id=da43fd21e94f6096670716f0a9f71549; path=/

Here, if Rails used :

Set-Cookie: \_session\_id=da43fd21e94f6096670716f0a9f71549; path=/app1

and in app2 later :

> Set-Cookie: \_session\_id=6df71f8e551b3b2de83d4fb0f43d7d56; path=/

Set-Cookie: \_session\_id=6df71f8e551b3b2de83d4fb0f43d7d56; path=/app2  
instead

You wouln’t have these problems.

I believe that using

ActionController::Base.session\_options[:session\_path] = “/app”

in environment.rb would solve your problem (not tested).

You could eliminate this problem by sharing the session store between  
your applications if need be (this require that you can indeed share  
the session data between your applications).

Lionel.

---

<div class="post-metadata">

**Author:** ![stepheneb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@stepheneb](https://www.ruby-forum.com/u/stepheneb)\
**Post date:** [February 10, 2007, 11:37am UTC](https://www.ruby-forum.com/t/how-can-i-change-the-value-of-the-session-key/85088/4 "2007-02-10T11:37:32Z")

</div>

Stephen B. wrote:

> How can I change the session key “\_session\_id” returned to the client  
> from my rails apps. I’m using both 1.2.2 and 1.1.6 frameworks.

Wouldn’t

class ApplicationController \< ActionController::Base  
# Pick a unique cookie name to distinguish our session data from  
others’  
session :session\_key =\> ‘\_some\_unique\_session\_id’  
end

do what you’re after?
