# One Session, Many Apps

**URL:** <https://www.ruby-forum.com/t/one-session-many-apps/109739>\
**Category:** Rails\
**Created:** [July 18, 2007, 2:46pm UTC](https://www.ruby-forum.com/t/one-session-many-apps/109739 "2007-07-18T14:46:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jarod\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@Jarod\_R](https://www.ruby-forum.com/u/Jarod_R)\
**Post date:** [July 18, 2007, 2:46pm UTC](https://www.ruby-forum.com/t/one-session-many-apps/109739/1 "2007-07-18T14:46:49Z")

</div>

I’m looking for a way to share one ActiveRecordStore session between  
multiple apps. I’m sure its possible, but not sure how. Ideas?

---

<div class="post-metadata">

**Author:** ![Jarod\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@Jarod\_R](https://www.ruby-forum.com/u/Jarod_R)\
**Post date:** [July 18, 2007, 2:58pm UTC](https://www.ruby-forum.com/t/one-session-many-apps/109739/2 "2007-07-18T14:58:09Z")

</div>

I guess this would hinge on setting a domain level cookie,  
as its the cookie which hooks in the session id.

You could then connect to a shared database for your session store

in databases.yml you’d have to define

sessions\_development:  
host: blah  
name: blah

sessions\_test:  
blah  
…

class ActiveRecordStore::Session  
establish\_connection(“sessions#{RAILS\_ENV}”.intern)  
end

and probably

class ActiveRecordStore::SqlBypass.connection =  
ActiveRecordStore::Session.connection

haven’t tried it,  
but it makes sense,  
I think.

Jarod R. wrote:

> I’m looking for a way to share one ActiveRecordStore session between  
> multiple apps. I’m sure its possible, but not sure how. Ideas?

---

<div class="post-metadata">

**Author:** ![Jarod\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@Jarod\_R](https://www.ruby-forum.com/u/Jarod_R)\
**Post date:** [July 18, 2007, 3:27pm UTC](https://www.ruby-forum.com/t/one-session-many-apps/109739/3 "2007-07-18T15:27:54Z")

</div>

On Jul 18, 2007, at 08:45 , Jarod R. wrote:

> I’m looking for a way to share one ActiveRecordStore session between  
> multiple apps. I’m sure its possible, but not sure how. Ideas?

Memcache. 🙂

If you use Memcache to store your sessions, then you can easily  
access data across your applications. You just need to make sure you  
don’t have namespace collisions or your data will get mixed up  
between the sites.

I know, not a very traditional solution to this problem, but for  
sites running on more than one server Memcache should be used anyway  
IMHO so you get the added benefit for free.

– Mitch

---

<div class="post-metadata">

**Author:** ![Jarod\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@Jarod\_R](https://www.ruby-forum.com/u/Jarod_R)\
**Post date:** [July 18, 2007, 2:59pm UTC](https://www.ruby-forum.com/t/one-session-many-apps/109739/4 "2007-07-18T14:59:25Z")

</div>

and of course,  
you get into a whole annoying world if your running your tests off  
multiple dbs.  
(you may just want to keep the same database for dev and test)

---

<div class="post-metadata">

**Author:** ![Jarod\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@Jarod\_R](https://www.ruby-forum.com/u/Jarod_R)\
**Post date:** [July 18, 2007, 4:02pm UTC](https://www.ruby-forum.com/t/one-session-many-apps/109739/5 "2007-07-18T16:02:10Z")

</div>

On Jul 18, 2007, at 6:46 AM, Jarod R. wrote:

> Thanks Mitch. Memcache would probably work however at the moment it  
> isn’t in the cards. The apps use the same database to store sessions.  
> The question is how to make the session persist across apps.

Short answer, you can’t 😕 Longer answer: It’s easy enough to share  
the database sessions table but the cookies that store the session\_id  
are tied to the domain they we’re served from. So the only way to  
share the sessons out of the box is to use subdomains and set the  
session cookie correctly.

set the sessions cookie domain to .example.com the . is important

Then you can use the same sessions cookies across subdomains like  
[foo.example.com](http://foo.example.com) [bar.example.com](http://bar.example.com)

Cheers-

– Ezra Z.  
– Founder & Ruby Hacker  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)

---

<div class="post-metadata">

**Author:** ![Jarod\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@Jarod\_R](https://www.ruby-forum.com/u/Jarod_R)\
**Post date:** [July 18, 2007, 4:06pm UTC](https://www.ruby-forum.com/t/one-session-many-apps/109739/6 "2007-07-18T16:06:11Z")

</div>

Ezra Z. wrote:

> the database sessions table but the cookies that store the session\_id  
> Cheers-

Ezra, thats more along the lines of what I thought it would be. And it  
works well enough for my needs. Out of curiosity, though, could one  
persist the actual object using DRB?

– Jarod

---

<div class="post-metadata">

**Author:** ![Jarod\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@Jarod\_R](https://www.ruby-forum.com/u/Jarod_R)\
**Post date:** [July 18, 2007, 3:52pm UTC](https://www.ruby-forum.com/t/one-session-many-apps/109739/7 "2007-07-18T15:52:09Z")

</div>

Mitch P. wrote:

> access data across your applications. You just need to make sure you

Thanks Mitch. Memcache would probably work however at the moment it  
isn’t in the cards. The apps use the same database to store sessions.  
The question is how to make the session persist across apps.

---

<div class="post-metadata">

**Author:** ![Jarod\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@Jarod\_R](https://www.ruby-forum.com/u/Jarod_R)\
**Post date:** [July 19, 2007, 12:59am UTC](https://www.ruby-forum.com/t/one-session-many-apps/109739/8 "2007-07-19T00:59:52Z")

</div>

The real answer to your question depends on what exactly you are  
trying to accomplish. Sharing sessions might be the best, or even a  
good solution. For example if your apps all require a login, just  
create a separate table to hold the information you want to share and  
key it off the username. Memcache would be perfect for this, not sure  
why you are ruling it out.

If your apps all require a login, and the shared data absolutely has  
to be saved on every request, then you can share sessions by creating  
a username column in the sessions table, and modify activerecord so  
when someone logs in it checks to see if a session exists for the  
user, and if so resets the cookie session id to that user. That way  
lies madness, but it can work in the right circumstances.

Chris
