# Active Record Session expiration in Rails 2.3

**URL:** <https://www.ruby-forum.com/t/active-record-session-expiration-in-rails-2-3/159198>\
**Category:** Rails\
**Created:** [February 2, 2009, 10:16am UTC](https://www.ruby-forum.com/t/active-record-session-expiration-in-rails-2-3/159198 "2009-02-02T10:16:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nauhaie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ab097/32.png) [@nauhaie](https://www.ruby-forum.com/u/nauhaie)\
**Post date:** [February 2, 2009, 10:16am UTC](https://www.ruby-forum.com/t/active-record-session-expiration-in-rails-2-3/159198/1 "2009-02-02T10:16:38Z")

</div>

Hi,

I cannot figure out how to let session cookies expire after let’s say  
two years. Note that I am _not_ using CookieStore. I mean the cookie  
referencing the Active Record Session.

Here is my config/environnement.rb

++++++++++++++

[…]

Rails::Initializer.run do |config|  
[…]  
config.action\_controller.session = {  
:key =\> “…”,  
:secret=\>"…",  
}

end

[…]

ActionController::Base.session\_store = :active\_record\_store  
ActionController::Base.session\_options[:session\_expires]=(Time.now+2.years)

++++++++++++++

Sessions are correctly stored in the database, but the cookie expires  
when the browser is quit. I tried everything!

I used to rely on SQLSessionStore, which let my cookies expire as I  
wanted, but since SQLSessionStore doesn’t seem to work with 2.3…

If someone could help, it would be great!

Thank you  
Nauhaie

---

<div class="post-metadata">

**Author:** ![nauhaie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ab097/32.png) [@nauhaie](https://www.ruby-forum.com/u/nauhaie)\
**Post date:** [February 2, 2009, 11:25am UTC](https://www.ruby-forum.com/t/active-record-session-expiration-in-rails-2-3/159198/2 "2009-02-02T11:25:06Z")

</div>

Session cookies are supposed to expire when you quit the browser!

Ordinary cookies however are another matter entirely. I asked a  
similar question (how to get non session cookies to be stored in the  
database) a few days ago and got no answer.

Best of luck.

---

<div class="post-metadata">

**Author:** ![nauhaie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ab097/32.png) [@nauhaie](https://www.ruby-forum.com/u/nauhaie)\
**Post date:** [February 2, 2009, 1:00pm UTC](https://www.ruby-forum.com/t/active-record-session-expiration-in-rails-2-3/159198/3 "2009-02-02T13:00:34Z")

</div>

Hi,

Thank you for your answer! However, I am sure it is possible to make  
these session cookies stay! The option exists, and it always worked for  
me until 2.3! I know ActiveRecord Session Store has been refactored in  
this version (at least renamed), and I am sure this is merely a question  
of setting the option in the wrong place!

Any idea?

Thank you  
Nauhaie

Peter H. wrote:

> Session cookies are supposed to expire when you quit the browser!
> 
> Ordinary cookies however are another matter entirely. I asked a  
> similar question (how to get non session cookies to be stored in the  
> database) a few days ago and got no answer.
> 
> Best of luck.

---

<div class="post-metadata">

**Author:** ![nauhaie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ab097/32.png) [@nauhaie](https://www.ruby-forum.com/u/nauhaie)\
**Post date:** [April 29, 2009, 12:41pm UTC](https://www.ruby-forum.com/t/active-record-session-expiration-in-rails-2-3/159198/4 "2009-04-29T12:41:04Z")

</div>

Hello mate,

I’m wonder how to use database sessions store instead of cookies.  
Rails 2.3

thx

On 2æœˆ2æ—¥, ä¸Šåˆ10æ—¶16åˆ†, Nauhaie N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![nauhaie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ab097/32.png) [@nauhaie](https://www.ruby-forum.com/u/nauhaie)\
**Post date:** [February 2, 2009, 1:12pm UTC](https://www.ruby-forum.com/t/active-record-session-expiration-in-rails-2-3/159198/5 "2009-02-02T13:12:05Z")

</div>

Oh, I found the reason

:session\_expires has been replaced by :expire\_after.

This now works:

ActionController::Base.session\_options[:expire\_after]=2.years

---

<div class="post-metadata">

**Author:** ![nauhaie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ab097/32.png) [@nauhaie](https://www.ruby-forum.com/u/nauhaie)\
**Post date:** [April 29, 2009, 3:01pm UTC](https://www.ruby-forum.com/t/active-record-session-expiration-in-rails-2-3/159198/6 "2009-04-29T15:01:18Z")

</div>

new on ror wrote:

> Hello mate,
> 
> I’m wonder how to use database sessions store instead of cookies.  
> Rails 2.3

## environment.rb

config.action\_controller.session\_store = :active\_record\_store

---

<div class="post-metadata">

**Author:** ![nauhaie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ab097/32.png) [@nauhaie](https://www.ruby-forum.com/u/nauhaie)\
**Post date:** [April 29, 2009, 3:20pm UTC](https://www.ruby-forum.com/t/active-record-session-expiration-in-rails-2-3/159198/7 "2009-04-29T15:20:45Z")

</div>

Robert W. wrote:

> new on ror wrote:
> 
> > Hello mate,
> > 
> > I’m wonder how to use database sessions store instead of cookies.  
> > Rails 2.3
> 
> ## environment.rb
> 
> config.action\_controller.session\_store = :active\_record\_store

What I wonder is what are the advantages of using an active record  
session store?

I assume people do this for a few reasons:

1. Need to persist sessions between browser launches. I would consider  
this in only in very rare cases. It’s my opinion that session cookies in  
their current form are even too lenient. An HTML 5 local session storage  
object seems like the ideal choice, given that they limit a session to a  
single browser window instance, having the same life-cycle and  
encapsulation as the window. They also seem to fix a common problem with  
cookies leaking out to other domains. Local storage objects are bound to  
the domain that created them without the loopholes cookies allow. Too  
bad this isn’t yet a viable option, but hopefully one day will be.

2. Need to store more that 4K of session data. In most cases this  
shouldn’t be an issue given that it’s good practice to keep sessions as  
small as possible. And again HTML 5 local session storage eliminates  
this limitation (something else to look forward to in the future).

3. Need to store sensitive data inside session objects. I cringe as I  
write that. It just sounds like such a bad idea. But, I suppose that’s  
why encryption was invented.

What are some other reasons I’ve not listed here?

---

<div class="post-metadata">

**Author:** ![nauhaie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ab097/32.png) [@nauhaie](https://www.ruby-forum.com/u/nauhaie)\
**Post date:** [August 11, 2009, 3:35pm UTC](https://www.ruby-forum.com/t/active-record-session-expiration-in-rails-2-3/159198/8 "2009-08-11T15:35:09Z")

</div>

> What I wonder is what are the advantages of using an active record  
> session store?
> 
> I assume people do this for a few reasons:
> 
> 1. Need to persist sessions between browser launches…ay will be.
> 
> 2. Need to store more that 4K of session data…
> 
> 3. Need to store sensitive data inside session objects…

1. Need multiple app servers to scale by using the same data store.

> What are some other reasons I’ve not listed here?

---

<div class="post-metadata">

**Author:** ![nauhaie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ab097/32.png) [@nauhaie](https://www.ruby-forum.com/u/nauhaie)\
**Post date:** [August 11, 2009, 3:37pm UTC](https://www.ruby-forum.com/t/active-record-session-expiration-in-rails-2-3/159198/9 "2009-08-11T15:37:45Z")

</div>

On Tue, Aug 11, 2009 at 10:35 AM, Marc  
Hassman[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. Need multiple app servers to scale by using the same data store.

Cookie based session store does that without hitting your database  
server.

- 

Maurício Linhares  
[http://codeshooter.wordpress.com/](http://codeshooter.wordpress.com/) | [http://twitter.com/mauriciojr](http://twitter.com/mauriciojr)
