# Session doesn't seem to store anything

**URL:** <https://www.ruby-forum.com/t/session-doesnt-seem-to-store-anything/111178>\
**Category:** Rails\
**Created:** [August 3, 2007, 3:39pm UTC](https://www.ruby-forum.com/t/session-doesnt-seem-to-store-anything/111178 "2007-08-03T15:39:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [August 3, 2007, 3:39pm UTC](https://www.ruby-forum.com/t/session-doesnt-seem-to-store-anything/111178/1 "2007-08-03T15:39:12Z")

</div>

I’m going to first frankly admit that this is the first web  
application I’m attempting to make for the first since the depot  
project in Agile Web D. with Ruby on Rails, and already, I’m  
lost.

Anyway, according to my debug window I quickly made in the view files,  
I can easily tell the sessions doesn’t seem to hold anything. Every  
time I change to another controller link, the session either  
disappear, or erase itself, then fill itself up with the values the  
controller said to fill.  
I used the Database version of sessions  
(session\_store=:active\_record\_store) (I also don’t know a thing about  
MySQL. I needed the sessions to expire within a certain time limit).  
To wrap up the things that could possibly be the problem:

1. Do I need to enable the session in the controller? I thought Rails  
did that for me by default.
2. Do I need to create a separate column for each hash value the  
session is going to store?
3. Did I initialize the session wrong? How can I check? I first  
typed in the command line, “rake db:sessions:create”, then “rake  
db:migrate”, so the table should be there…
4. Where can I learn a little bit about MySQL? I’m a little  
intimidated by their homepage, yet I need to find a way to create a  
sweeper for the session table.
5. If none of these answers solves the problem, what may be another  
solution to it?

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [August 3, 2007, 5:00pm UTC](https://www.ruby-forum.com/t/session-doesnt-seem-to-store-anything/111178/2 "2007-08-03T17:00:22Z")

</div>

OK, I’ll try that. Many thanks! Out of curiosity, what  
does :session\_key do? Is it something like a security thing for  
sessions or something?

---

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [August 3, 2007, 4:21pm UTC](https://www.ruby-forum.com/t/session-doesnt-seem-to-store-anything/111178/3 "2007-08-03T16:21:18Z")

</div>

On 03 Aug 2007, at 15:38, Taro wrote:

> 1. Do I need to enable the session in the controller? I thought Rails  
> did that for me by default.

Just comment out the :session\_key line in the application controller  
and see if that solves it.

> 1. Do I need to create a separate column for each hash value the  
> session is going to store?

No

> 1. Did I initialize the session wrong? How can I check? I first  
> typed in the command line, “rake db:sessions:create”, then “rake  
> db:migrate”, so the table should be there…

Make sure you set the session store to active\_record\_store

> 1. Where can I learn a little bit about MySQL? I’m a little  
> intimidated by their homepage, yet I need to find a way to create a  
> sweeper for the session table.

The easiest way to do it, is pick out a method that’s going to be  
called on regular occasions (such as the login method) and do the  
session cleanup there (as long as you don’t have thousands of  
concurrent requests, the cleanup won’t slow down the site):

Put this in environment.rb (at the bottom)

# Method for cleaning up stale sessions

class SessionCleanup  
def self.cleanup  
CGI::Session::ActiveRecordStore::Session.destroy\_all(  
[‘updated\_at \< ?’, 8.hours.ago]  
)  
end  
end

Then in your login method:

def login  
SessionCleanup.cleanup  
…  
end

Best regards

Peter De Berdt
