# Session not persisting

**URL:** <https://www.ruby-forum.com/t/session-not-persisting/61872>\
**Category:** Rails\
**Created:** [May 29, 2006, 7:32am UTC](https://www.ruby-forum.com/t/session-not-persisting/61872 "2006-05-29T07:32:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wmerydith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ccd318/32.png) [@wmerydith](https://www.ruby-forum.com/u/wmerydith)\
**Post date:** [May 29, 2006, 7:32am UTC](https://www.ruby-forum.com/t/session-not-persisting/61872/1 "2006-05-29T07:32:33Z")

</div>

I set up session to save to the db and everytime I hit a page I get a  
new session entry created. Some of them have session\_ids of more than 5  
digits, but lots of them have session\_ids like 0.

Something is amiss.

---

<div class="post-metadata">

**Author:** ![wmerydith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ccd318/32.png) [@wmerydith](https://www.ruby-forum.com/u/wmerydith)\
**Post date:** [May 29, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/session-not-persisting/61872/2 "2006-05-29T17:08:03Z")

</div>

I built a test application and get the same behavior. I cannot find any  
documentation of this bug so I assume I’ve done something wrong.

I did follow the instructions in ‘Agile Web D. with Rails’ step  
by step though.

---

<div class="post-metadata">

**Author:** ![wmerydith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ccd318/32.png) [@wmerydith](https://www.ruby-forum.com/u/wmerydith)\
**Post date:** [May 29, 2006, 5:14pm UTC](https://www.ruby-forum.com/t/session-not-persisting/61872/3 "2006-05-29T17:14:42Z")

</div>

can you paste the lines from environment.rb that you changed to put  
sessions  
in the DB ?

How did you create your session table in the DB ?

adam

---

<div class="post-metadata">

**Author:** ![wmerydith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ccd318/32.png) [@wmerydith](https://www.ruby-forum.com/u/wmerydith)\
**Post date:** [May 29, 2006, 5:37pm UTC](https://www.ruby-forum.com/t/session-not-persisting/61872/4 "2006-05-29T17:37:52Z")

</div>

# Use the database for sessions instead of the file system

# (create the session table with ‘rake db:sessions:create’)

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

class AddSessions \< ActiveRecord::Migration  
def self.up  
create\_table :sessions do |t|  
t.column :session\_id, :string  
t.column :data, :text  
t.column :updated\_at, :datetime  
end

```
add_index :sessions, :session_id

```

end

def self.down  
drop\_table :sessions  
end  
end

---

<div class="post-metadata">

**Author:** ![wmerydith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ccd318/32.png) [@wmerydith](https://www.ruby-forum.com/u/wmerydith)\
**Post date:** [May 29, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/session-not-persisting/61872/5 "2006-05-29T18:02:56Z")

</div>

Here is the code in my controller that gets called often to store a  
Score object to the session:

private

# Return the Score from this session, or create a new Score

def find\_score  
session[:score] ||= Score.new  
end

Here is my Score object:

class Score

include Reloadable

attr\_reader :point\_ids, :logged\_test\_id

def initialize  
@point\_ids = []  
@logged\_test\_id  
end

def set\_logged\_test\_id(logged\_test\_id)  
@logged\_test\_id = logged\_test\_id  
end

def add\_point\_id(point\_id)  
@point\_ids \<\< point\_id  
end

end
