# Sharing a variable between methods?

**URL:** <https://www.ruby-forum.com/t/sharing-a-variable-between-methods/124259>\
**Category:** Rails\
**Created:** [December 27, 2007, 5:14am UTC](https://www.ruby-forum.com/t/sharing-a-variable-between-methods/124259 "2007-12-27T05:14:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gedeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@Gedeon](https://www.ruby-forum.com/u/Gedeon)\
**Post date:** [December 27, 2007, 5:14am UTC](https://www.ruby-forum.com/t/sharing-a-variable-between-methods/124259/1 "2007-12-27T05:14:48Z")

</div>

Hello!

I need some help on something that seems very basic and ‘noobish’ to  
me but that I cant figure out…  
I’m going to try to make it as simple as possible…

I have a method that initializes an array. Let’s call it "main\_view’.  
Why? Because it is the main view.  
The corresponding view - main\_view.rhtml - does not do anything with  
this array, instead, it contains links to ajax-powered partials that  
let users alter this array in different ways, depending on what link  
they click on.  
If my page was made in ascii-‘art’ and I had a 2 minutes deadline, it  
would probably look as bad as this:

* * *

Add elements  
Remove elements  
Swap elements

* * *

Partial to  
let user  
alter the array

* * *

* * *

So, when I click on a link, it goes to, for example, an “add\_element”  
method and loads the partial.  
Then I edit/alter/… the array, + all those things users have to do.  
Then, once i am done, it goes to an “update\_array” method that gets  
the data from the partial and updates the array with it.

Now, the big problem i am facing :

I cannot find how to cleanly and elegantly pass the array from my  
‘main\_view’ method to the ‘add\_element’ (or other), then to the  
‘update’ method and finally back to ‘main\_view’ for another round of  
modifications.

I could probably pass it like this, using hidden fields or whatever  
comes to my mind :

main\_view method =\> main\_view.rhtml  
_user clicks on add element_ =\> add\_element method =\>  
\_add\_element.rhtml  
_user updates the array from the view and clicks on update_ =\> update  
method =\> main\_view.rhtml =\> …  
But just thinking about how ugly it is is gonna make me have  
nightmares tonight.

Or… I could make this array a sessions variable, but it’s not clean  
either as it might stay in the session variable for ever… And i dont  
want dust on my variables.

So, is there any nice way of doing this? Am i missing something here,  
some part of Rails’ magic?  
Any help would be greatly appreciated, I am kinda tired and dont want  
to have nightmares tonight 🙂

Thank you!

Gedeon

---

<div class="post-metadata">

**Author:** ![Gedeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@Gedeon](https://www.ruby-forum.com/u/Gedeon)\
**Post date:** [December 27, 2007, 5:20am UTC](https://www.ruby-forum.com/t/sharing-a-variable-between-methods/124259/2 "2007-12-27T05:20:04Z")

</div>

## You could use a session variable and store it all in that.

Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![Gedeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@Gedeon](https://www.ruby-forum.com/u/Gedeon)\
**Post date:** [December 27, 2007, 6:08am UTC](https://www.ruby-forum.com/t/sharing-a-variable-between-methods/124259/3 "2007-12-27T06:08:00Z")

</div>

Thank you Ryan,  
Yes, I could do that but from what i read, it is not recommended  
because of unexpired sessions that will pile up and could create a  
performance problem… Which is why I am posting here.

It should be ok for my project as it will have a limited number of  
users, but still, I’d rather like to make it clean and scalable…  
Besides, I’m sure I’ll bump into this problem again on other projects  
so I’ll need a better solution…

Gedeon

---

<div class="post-metadata">

**Author:** ![Gedeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@Gedeon](https://www.ruby-forum.com/u/Gedeon)\
**Post date:** [December 27, 2007, 6:19am UTC](https://www.ruby-forum.com/t/sharing-a-variable-between-methods/124259/4 "2007-12-27T06:19:07Z")

</div>

@Gedeon:

For something like this, I would definitely use the session. The purpose  
of  
the session is to persist data between requests. Do not worry about  
unexpired sessions… unless you have explicitly turned sessions OFF in  
your  
controllers, you’re already creating them. Investigate database session  
storage or cookie-based storage as they each have benefits and  
disadvantages.

---

<div class="post-metadata">

**Author:** ![Gedeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@Gedeon](https://www.ruby-forum.com/u/Gedeon)\
**Post date:** [December 27, 2007, 6:12am UTC](https://www.ruby-forum.com/t/sharing-a-variable-between-methods/124259/5 "2007-12-27T06:12:41Z")

</div>

Store the object in a database and then link to directly to a user? I  
need  
to understand what you’re trying to do first.

On Dec 27, 2007 3:37 PM, Gedeon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Gedeon
> 
> On Dec 27, 11:19 am, “Ryan B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ## You could use a session variable and store it all in that.
> > 
> > Ryan B.[http://www.frozenplague.net](http://www.frozenplague.net)  
> > Feel free to add me to MSN and/or GTalk as this email.

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![Gedeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@Gedeon](https://www.ruby-forum.com/u/Gedeon)\
**Post date:** [December 27, 2007, 6:26am UTC](https://www.ruby-forum.com/t/sharing-a-variable-between-methods/124259/6 "2007-12-27T06:26:42Z")

</div>

I guess I’ll do that then 🙂  
I am already using the session variable but only for ‘small’ things,  
such as the currently logged in user id, selected css id, …  
For some reason, I was scared i might cause some kind of meltdown,  
lose my hair,… or even worse by storing ‘bigger’ variables in there.

Thank you,

Gedeon

---

<div class="post-metadata">

**Author:** ![Gedeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@Gedeon](https://www.ruby-forum.com/u/Gedeon)\
**Post date:** [December 27, 2007, 7:16am UTC](https://www.ruby-forum.com/t/sharing-a-variable-between-methods/124259/7 "2007-12-27T07:16:23Z")

</div>

On Dec 26, 2007, at 11:26 PM, Gedeon wrote:

> I guess I’ll do that then 🙂  
> I am already using the session variable but only for ‘small’ things,

I was doing that for a while, but with large arrays things would get  
clobbered and I’d get a 500 error. What I did instead is put enough  
info in the the session to know how to rebuild the array.

---

<div class="post-metadata">

**Author:** ![Gedeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@Gedeon](https://www.ruby-forum.com/u/Gedeon)\
**Post date:** [January 3, 2008, 1:53pm UTC](https://www.ruby-forum.com/t/sharing-a-variable-between-methods/124259/8 "2008-01-03T13:53:18Z")

</div>

Hello

You could investigate nondatabase Model too … this model object could  
act as a wrapper for your array and persist between request (you may  
or may not store it in the session)

Don’t really know if it would fit, you’ll have to investigate a bit.

Gilles.

---

<div class="post-metadata">

**Author:** ![Gedeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@Gedeon](https://www.ruby-forum.com/u/Gedeon)\
**Post date:** [December 27, 2007, 6:28am UTC](https://www.ruby-forum.com/t/sharing-a-variable-between-methods/124259/9 "2007-12-27T06:28:49Z")

</div>

If the list they are creating is important enough that it should be  
stored in the database and displayed as a list to the user using  
something such as the “acts\_as\_list” plugin. If the list is not database  
worthy, then store it in the session.

In Rails 2.0, session data is actually stored as an encrypted cookie on  
the client computer by default. All the data is stored on the client  
computer and as such there is no “cruft” to build up. Not to mention,  
you can clear the session data at any time once they are finished.

There is really only five ways to persist data between actions (read:  
requests) :

1. Session
2. Cookies
3. Database
4. Files
5. Background Process

For your needs, it seems that persisting an array would be best suited  
in the session or in the database.
