# Saving instance variables between GETs

**URL:** <https://www.ruby-forum.com/t/saving-instance-variables-between-gets/134848>\
**Category:** Rails\
**Created:** [April 15, 2008, 5:00pm UTC](https://www.ruby-forum.com/t/saving-instance-variables-between-gets/134848 "2008-04-15T17:00:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [April 15, 2008, 5:00pm UTC](https://www.ruby-forum.com/t/saving-instance-variables-between-gets/134848/1 "2008-04-15T17:00:08Z")

</div>

Am I approaching this the wrong way according to the Ruby on Rails  
conventions?

I will explain the situation. I currently have a reporting system which  
will create and eventually render a report based on some choices the  
user has made in a GET request and eventually end up with a @report  
object.

Depending on what report the user wants, the @report object contains  
different structures. Now there is a button on the same site, which is  
called download report. Once this is called a different function in the  
controller is called and I no longer have the @report object. This  
forces me to recreate the same @report object based on model data which  
takes quite a lot of processing time.

Is there any way to save this object between the different controller  
functions? The first thing that comes to mind is the session hash.  
Storing the @report in here though corrupts the session and causes an  
error when it is used again (every next call) because it can not  
determine the type of the @report object.

How is a good RoR programmer supposed to tackle this problem?

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [April 17, 2008, 6:26pm UTC](https://www.ruby-forum.com/t/saving-instance-variables-between-gets/134848/2 "2008-04-17T18:26:57Z")

</div>

Am I really the only one wondering about this? Or is my basic structure  
of the application wrong?

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [April 17, 2008, 6:41pm UTC](https://www.ruby-forum.com/t/saving-instance-variables-between-gets/134848/3 "2008-04-17T18:41:31Z")

</div>

On Apr 17, 2008, at 12:26 PM, Chris D. wrote:

> ## Am I really the only one wondering about this? Or is my basic structure of the application wrong?

Your assumption of the basic structure is wrong. The HTTP protocol is  
stateless so you need to use sessions (cookies, and possibly a  
database) to establish the illusion of persistence. Every new request  
creates a new instance of the controller that needs to handle it so  
the instance variables (and the instance) in the previous request are  
gone.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
