# Using same variable in controller

**URL:** <https://www.ruby-forum.com/t/using-same-variable-in-controller/163915>\
**Category:** Rails\
**Created:** [March 26, 2009, 3:53am UTC](https://www.ruby-forum.com/t/using-same-variable-in-controller/163915 "2009-03-26T03:53:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arghx1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aca169/32.png) [@arghx1](https://www.ruby-forum.com/u/arghx1)\
**Post date:** [March 26, 2009, 3:53am UTC](https://www.ruby-forum.com/t/using-same-variable-in-controller/163915/1 "2009-03-26T03:53:36Z")

</div>

Hi guys! More question :P~

OK, I’m following a tutorial for rail, and I got this new.html.erb in  
views, and in order for a form to be showed up correctly without error,  
I had to add this small snippet of code in the controller under method  
“def new”.

class AdsController \< ApplicationController  
2 def show  
3 @ad = Ad.find(params[:id])  
4 end  
5  
6 def index  
7 @ads = Ad.find(:all)  
8 end  
9  
10 def new  
11 @ad = Ad.new  
12 end  
13 end

My question is, I thought this would be creating an error because I have  
def show using variable @ad, and then def new also using variable @ad,  
but instead of any glitch, I got the rail app works out perfectly.

I’m new to ruby and the programming world and especially rail. I just  
wonder is this a good practice? It’s like overwrite what @ad in “def  
show” when user wants to get action “def new.” Or maybe I got confused?  
Rails has all variable in each method as local? Or each time each  
action/method gets executed, it will be newly object in memory anyway,  
and so it won’t be affected by the code/abstract above? Please help me  
understand this, thanks…

---

<div class="post-metadata">

**Author:** ![arghx1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aca169/32.png) [@arghx1](https://www.ruby-forum.com/u/arghx1)\
**Post date:** [March 26, 2009, 4:05am UTC](https://www.ruby-forum.com/t/using-same-variable-in-controller/163915/2 "2009-03-26T04:05:24Z")

</div>

Power O. wrote:

> My question is, I thought this would be creating an error because I have  
> def show using variable @ad, and then def new also using variable @ad,  
> but instead of any glitch, I got the rail app works out perfectly.

Welcome to “event driven” land.

If you wrote a new class, and did not inherit ApplicationController, you  
would  
create class instances with .new.

```
x = MyClass.new

```

Instance variables - beginning with @ - would live as long as x did.

A Controller class is different. Rails creates an instance for you, each  
time  
the routes.rb map tells Rails to. Then this object lives long enough to  
service  
one and only one action.

Then it dies. All its instance variables go away, too.

So you can declare different sets of instance variables - or even the  
same set -  
in each action, and Rails generally keeps them out of each others’ hair.

Now please read a book on Ruby, straight thru. Typing in tutorials is  
fun, but  
it burdens this newsgroup with content that you should be learning for  
yourself!

–  
Phlip

---

<div class="post-metadata">

**Author:** ![arghx1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aca169/32.png) [@arghx1](https://www.ruby-forum.com/u/arghx1)\
**Post date:** [March 26, 2009, 4:41am UTC](https://www.ruby-forum.com/t/using-same-variable-in-controller/163915/3 "2009-03-26T04:41:03Z")

</div>

Sent from my iPhone

On Mar 25, 2009, at 7:53 PM, Power O. \<rails-mailing-list@andreas-  
[s.net](http://s.net)\> wrote:

> ## 2 def show 13 end Rails has all variable in each method as local? Or each time each action/method gets executed, it will be newly object in memory anyway, and so it won’t be affected by the code/abstract above? Please help me understand this, thanks…
> 
> Posted via [http://www.ruby-forum.com](http://www.ruby-forum.com)

Andreas, the instance variable is set when the action is invoked. For  
example,

[http://www.example.com/controller/action](http://www.example.com/controller/action)

Thus, I would recommend reading one of the relevant guides at

> **[Ruby on Rails Guides](https://guides.rubyonrails.org)**
>
> Ruby on Rails Guides

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![arghx1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aca169/32.png) [@arghx1](https://www.ruby-forum.com/u/arghx1)\
**Post date:** [March 26, 2009, 6:13pm UTC](https://www.ruby-forum.com/t/using-same-variable-in-controller/163915/4 "2009-03-26T18:13:52Z")

</div>

Phlip wrote:

> A Controller class is different. Rails creates an instance for you, each  
> time  
> the routes.rb map tells Rails to. Then this object lives long enough to  
> service  
> one and only one action.
> 
> Then it dies. All its instance variables go away, too.

Even if this were not the case reusing that instance variable would not  
cause any errors or problems.

When the action is called the variable gets assigned a value. If the  
instance variable already had a value then the old value would be  
dereferenced by the assignment. Then the variable would be set to  
reference the new value. The memory that was previously used by the old  
value would subsequently be freed by the Ruby automatic garbage  
collector.
