# Newbie question; best way to store simple session data?

**URL:** <https://www.ruby-forum.com/t/newbie-question-best-way-to-store-simple-session-data/103053>\
**Category:** Rails\
**Created:** [May 1, 2007, 7:28am UTC](https://www.ruby-forum.com/t/newbie-question-best-way-to-store-simple-session-data/103053 "2007-05-01T07:28:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@ken](https://www.ruby-forum.com/u/ken)\
**Post date:** [May 1, 2007, 7:28am UTC](https://www.ruby-forum.com/t/newbie-question-best-way-to-store-simple-session-data/103053/1 "2007-05-01T07:28:28Z")

</div>

As a first test project, I’m writing a little Rails app that simply  
lets one browse the directory structure on the server. (Obviously,  
this isn’t intended for public deployment 🙂 ). One thing that needs  
to be stored in a session is the currently browsed directory. What’s  
the best way to store such simple data? (I sorta think a database  
table is overkill 🙂 )

Thanks,  
Ken

---

<div class="post-metadata">

**Author:** ![ken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@ken](https://www.ruby-forum.com/u/ken)\
**Post date:** [May 1, 2007, 7:32am UTC](https://www.ruby-forum.com/t/newbie-question-best-way-to-store-simple-session-data/103053/2 "2007-05-01T07:32:28Z")

</div>

ken wrote:

> As a first test project, I’m writing a little Rails app that simply  
> lets one browse the directory structure on the server. (Obviously,  
> this isn’t intended for public deployment 🙂 ). One thing that needs  
> to be stored in a session is the currently browsed directory. What’s  
> the best way to store such simple data? (I sorta think a database  
> table is overkill 🙂 )

Store it in a cookie.

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![ken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@ken](https://www.ruby-forum.com/u/ken)\
**Post date:** [May 1, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/newbie-question-best-way-to-store-simple-session-data/103053/3 "2007-05-01T18:01:23Z")

</div>

Rails has a session management mechanism already built for you. Just  
use that. Store your value in the session hash then you can just  
configure Rails to store sessions either in a file (the default) or in  
the database if you prefer that.

## Excerpt from Agile Web D. with Rails page 105:

## By default, Rails stores session information in a file on the server. If you have a single Rails server running, there’s no problem with this. But imagine that your store application gets so wildly popular that you run out of capacity on a single-server machine and need to run multiple boxes. The first request from a particular user might be routed to one back-end machine, but the second request might go to another. The session data stored on the first server isn’t available on the second; the user will get very confused as items appear and disappear in their cart across requests.

If you choose to store session data in the database just do this:  
depot\> rake db:sessions:create  
exists db/migrate  
create db/migrate/004\_add\_sessions.rb  
depot\> rake db:migrate

And here’s a sample of storing an object in the session:

def find\_cart  
unless session[:cart] # if there’s no cart in the  
session  
session[:cart] = Cart.new # add a new one  
end  
session[:cart] # return existing or new cart  
end
