# Newbie: Rails Controller Attribute Scope

**URL:** <https://www.ruby-forum.com/t/newbie-rails-controller-attribute-scope/54573>\
**Category:** Ruby\
**Created:** [March 8, 2006, 9:11pm UTC](https://www.ruby-forum.com/t/newbie-rails-controller-attribute-scope/54573 "2006-03-08T21:11:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 8, 2006, 9:11pm UTC](https://www.ruby-forum.com/t/newbie-rails-controller-attribute-scope/54573/1 "2006-03-08T21:11:42Z")

</div>

I’m having a hard time figguring out what is wrong with my controller.  
I create an attribure, @processes in the method index. I then want to  
access the attribute in the same class, in another method  
process\_status. I get an error:

NoMethodError in Processes#process\_status  
You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occured while evaluating nil.sizeRAILS\_ROOT:  
…/script/…/config/…

So it looks to me that the instance of ProcessController is changing  
and the attribute is nil? Is there a way around this? Why does Rails  
create a new controller for each invocation of a method?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 8, 2006, 9:51pm UTC](https://www.ruby-forum.com/t/newbie-rails-controller-attribute-scope/54573/2 "2006-03-08T21:51:08Z")

</div>

On 3/8/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> …/script/…/config/…
> 
> So it looks to me that the instance of ProcessController is changing  
> and the attribute is nil? Is there a way around this? Why does Rails  
> create a new controller for each invocation of a method?

This is probably better answered on the Rails list, but I’ll take a  
shot. I  
assume by new invocation of a method, you mean new request, correct? If  
so,  
then the answer is simply because on each request the entire rails  
environment is setup, and torn down. For any value to survive between  
requests it needs to either be in the flash, session, request, or  
external  
storage. This is probably a somewhat surprising behavior if you come  
from a  
servlet world, but in the PHP or PERL world, this is fairly common.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 8, 2006, 10:00pm UTC](https://www.ruby-forum.com/t/newbie-rails-controller-attribute-scope/54573/3 "2006-03-08T22:00:16Z")

</div>

removed\_email\_address@domain.invalid wrote:

> So it looks to me that the instance of ProcessController is changing  
> and the attribute is nil? Is there a way around this? Why does Rails  
> create a new controller for each invocation of a method?

In Rails unlike in Struts a new (statefull) instance of your Controller  
is created on each request, so you can acces instance variables between  
methods only in a single request process.  
If you want to save a state (i.e. share values between requests) use  
session instead.

I prefer this type of state management since you dont have to pass  
patameters between methods, and creation of new instance on each request  
lets you forget about purging them 😃 (GC wouldnt be able to collect  
them)

lopex
