# How to store some data in session

**URL:** <https://www.ruby-forum.com/t/how-to-store-some-data-in-session/92339>\
**Category:** Rails\
**Created:** [April 18, 2007, 1:00pm UTC](https://www.ruby-forum.com/t/how-to-store-some-data-in-session/92339 "2007-04-18T13:00:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stanly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@stanly](https://www.ruby-forum.com/u/stanly)\
**Post date:** [April 18, 2007, 1:00pm UTC](https://www.ruby-forum.com/t/how-to-store-some-data-in-session/92339/1 "2007-04-18T13:00:08Z")

</div>

Hi everyone,

I wanted to know how do i save some data in session and later retrieve  
that.  
I am new to rails and just couldn’t find any help regarding this.

Thank you

---

<div class="post-metadata">

**Author:** ![stanly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@stanly](https://www.ruby-forum.com/u/stanly)\
**Post date:** [April 18, 2007, 1:32pm UTC](https://www.ruby-forum.com/t/how-to-store-some-data-in-session/92339/2 "2007-04-18T13:32:15Z")

</div>

Qasim Ali wrote:

> Hi everyone,
> 
> I wanted to know how do i save some data in session and later retrieve  
> that.  
> I am new to rails and just couldn’t find any help regarding this.
> 
> Thank you

I’d suggest you make a new table to store the session ‘object’ (like,  
say, baskets if its a shopping basket).

Then you just do

session[:my\_basket\_id] = @basket.id

You are _strongly_ advised against just doing session[:basket] =  
@basket, and storing just the id in the session instead.

Does this make sense ?

Alan

---

<div class="post-metadata">

**Author:** ![stanly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@stanly](https://www.ruby-forum.com/u/stanly)\
**Post date:** [April 18, 2007, 3:29pm UTC](https://www.ruby-forum.com/t/how-to-store-some-data-in-session/92339/3 "2007-04-18T15:29:05Z")

</div>

Alan F. wrote:

> Qasim Ali wrote:
> 
> > Hi everyone,
> > 
> > I wanted to know how do i save some data in session and later retrieve  
> > that.  
> > I am new to rails and just couldn’t find any help regarding this.
> > 
> > Thank you
> 
> I’d suggest you make a new table to store the session ‘object’ (like,  
> say, baskets if its a shopping basket).
> 
> Then you just do
> 
> session[:my\_basket\_id] = @basket.id
> 
> You are _strongly_ advised against just doing session[:basket] =  
> @basket, and storing just the id in the session instead.
> 
> Does this make sense ?
> 
> Alan

Thank you Alan for your quick response. But why should we go for the  
database solution instead of simply utilizing the session variable, is  
there some specific reason for not using the session[:basket] = …

Thanks

---

<div class="post-metadata">

**Author:** ![stanly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@stanly](https://www.ruby-forum.com/u/stanly)\
**Post date:** [April 18, 2007, 3:40pm UTC](https://www.ruby-forum.com/t/how-to-store-some-data-in-session/92339/4 "2007-04-18T15:40:28Z")

</div>

Qasim Ali wrote:

> Alan F. wrote:
> 
> > Qasim Ali wrote:
> > 
> > > Hi everyone,
> > > 
> > > I wanted to know how do i save some data in session and later retrieve  
> > > that.  
> > > I am new to rails and just couldn’t find any help regarding this.
> > > 
> > > Thank you
> > 
> > I’d suggest you make a new table to store the session ‘object’ (like,  
> > say, baskets if its a shopping basket).
> > 
> > Then you just do
> > 
> > session[:my\_basket\_id] = @basket.id
> > 
> > You are _strongly_ advised against just doing session[:basket] =  
> > @basket, and storing just the id in the session instead.
> > 
> > Does this make sense ?
> > 
> > Alan
> 
> Thank you Alan for your quick response. But why should we go for the  
> database solution instead of simply utilizing the session variable, is  
> there some specific reason for not using the session[:basket] = …
> 
> Thanks

See here: [http://wiki.rubyonrails.org/rails/pages/sessions](http://wiki.rubyonrails.org/rails/pages/sessions)

Mostly to do with objects being stale, some security issues, and the  
sheer size of the sessions.

A.
