# Persisting an object

**URL:** <https://www.ruby-forum.com/t/persisting-an-object/184323>\
**Category:** Rails\
**Created:** [February 28, 2010, 11:11pm UTC](https://www.ruby-forum.com/t/persisting-an-object/184323 "2010-02-28T23:11:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rajinder\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@Rajinder\_Y](https://www.ruby-forum.com/u/Rajinder_Y)\
**Post date:** [February 28, 2010, 11:11pm UTC](https://www.ruby-forum.com/t/persisting-an-object/184323/1 "2010-02-28T23:11:26Z")

</div>

ok this is really basic rails question, but how do I keep an object  
alive between controller action? I thought I could create an object and  
it would remain alive, but I am guessing the controller object gets  
recreated each time?

–  
Kind Regards,  
Rajinder Y.

---

<div class="post-metadata">

**Author:** ![Rajinder\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@Rajinder\_Y](https://www.ruby-forum.com/u/Rajinder_Y)\
**Post date:** [February 28, 2010, 11:31pm UTC](https://www.ruby-forum.com/t/persisting-an-object/184323/2 "2010-02-28T23:31:20Z")

</div>

On Sun, Feb 28, 2010 at 2:09 PM, Rajinder Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ok this is really basic rails question, but how do I keep an object alive  
> between controller action? I thought I could create an object and it would  
> remain alive, but I am guessing the controller object gets recreated each  
> time?

Rajinder, you can store the object in a session. For example, you can  
do  
the following:

Store:

session[:object\_to\_persist] = an\_object

Retrieve:

some\_local\_variable = session[:object\_to\_persist]

For further information, I would recommend reading the relevant section  
from  
“Agile Web D. with Rails 3rd” by Dave T. et al.

Good look,

-Conrad

---

<div class="post-metadata">

**Author:** ![Rajinder\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@Rajinder\_Y](https://www.ruby-forum.com/u/Rajinder_Y)\
**Post date:** [February 28, 2010, 11:37pm UTC](https://www.ruby-forum.com/t/persisting-an-object/184323/3 "2010-02-28T23:37:46Z")

</div>

Conrad T. wrote:

> do the following:  
> from “Agile Web D. with Rails 3rd” by Dave T. et al.  
> Hi Conrad,

thanks I was not sure session object was the right way to do things.  
I’ve got 3 books on Rails and I’m hoping to get “Agile Web D.  
with Rails 3rd” soon =) but I’ll will check out the section now.

---

<div class="post-metadata">

**Author:** ![Rajinder\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@Rajinder\_Y](https://www.ruby-forum.com/u/Rajinder_Y)\
**Post date:** [March 1, 2010, 12:29am UTC](https://www.ruby-forum.com/t/persisting-an-object/184323/4 "2010-03-01T00:29:50Z")

</div>

Conrad T. wrote:

> ```
> object
> Store:
> 
> ```
> 
> later section(s) in the book that goes into greater detail.  
> Conrad, thanks again =) it’s working like a charm and I’ll read the  
> entire section on “Cart Creation” tonight!

Kind Regards,  
Rajinder Y.

---

<div class="post-metadata">

**Author:** ![Rajinder\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@Rajinder\_Y](https://www.ruby-forum.com/u/Rajinder_Y)\
**Post date:** [February 28, 2010, 11:51pm UTC](https://www.ruby-forum.com/t/persisting-an-object/184323/5 "2010-02-28T23:51:48Z")

</div>

On Sun, Feb 28, 2010 at 2:36 PM, Rajinder Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> >

Rajinder, you can read the section on “Cart Creation”. Also, there’s  
later  
section(s) in the book that goes into greater detail.

Good luck,

-Conrad
