# Singleton Object nil

**URL:** <https://www.ruby-forum.com/t/singleton-object-nil/172528>\
**Category:** Rails\
**Created:** [July 22, 2009, 11:05pm UTC](https://www.ruby-forum.com/t/singleton-object-nil/172528 "2009-07-22T23:05:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![knappe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@knappe](https://www.ruby-forum.com/u/knappe)\
**Post date:** [July 22, 2009, 11:05pm UTC](https://www.ruby-forum.com/t/singleton-object-nil/172528/1 "2009-07-22T23:05:15Z")

</div>

Hey all,

I have the following in my Controller:

```
    class << self
            attr_accessor :all_devices, :test
            #def initialize(devices)
            # @all_devices = devices
            #end
    end

```

def initialize\_devices  
AllDevicesController.all\_devices = @devices\_array  
redirect\_to :action =\> “show\_all\_devices”  
end

Where devices\_array is an array of devices.

def show\_all\_devices  
@devices\_array = AllDevicesController.all\_devices  
respond\_to do |format|  
format.html # show.html.erb  
format.xml { render :xml =\> @devices }  
end  
end

The problem here is when accessing @devices\_array in the view  
@devices\_array is nil. I _know_ the code is being executed in  
initialize\_devices because if I don’t redirect to show\_all\_devices I can  
manipulate the @devices\_array within the initialize\_devices view.  
However, accessing AllDevicesController.all\_devices in show\_all\_devices  
results in nil.

Anyone know why?

---

<div class="post-metadata">

**Author:** ![knappe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@knappe](https://www.ruby-forum.com/u/knappe)\
**Post date:** [July 23, 2009, 10:06am UTC](https://www.ruby-forum.com/t/singleton-object-nil/172528/2 "2009-07-23T10:06:31Z")

</div>

On Jul 22, 10:05 pm, Tyler K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> The problem here is when accessing @devices\_array in the view  
> @devices\_array is nil. I _know_ the code is being executed in  
> initialize\_devices because if I don’t redirect to show\_all\_devices I can  
> manipulate the @devices\_array within the initialize\_devices view.  
> However, accessing AllDevicesController.all\_devices in show\_all\_devices  
> results in nil.
> 
> Anyone know why?

because in development mode your source code is reloaded between  
requests (ie it is no longer the same AllDevicesController class).

Fred

---

<div class="post-metadata">

**Author:** ![knappe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@knappe](https://www.ruby-forum.com/u/knappe)\
**Post date:** [July 22, 2009, 11:14pm UTC](https://www.ruby-forum.com/t/singleton-object-nil/172528/3 "2009-07-22T23:14:42Z")

</div>

Tyler K. wrote:

> Hey all,
> 
> I have the following in my Controller:
> 
> ```
> class << self
> attr_accessor :all_devices, :test
> #def initialize(devices)
> # @all_devices = devices
> #end
> end
> 
> ```
> 
> def initialize\_devices  
> AllDevicesController.all\_devices = @devices\_array  
> redirect\_to :action =\> “show\_all\_devices”  
> end
> 
> Where devices\_array is an array of devices.
> 
> def show\_all\_devices  
> @devices\_array = AllDevicesController.all\_devices  
> respond\_to do |format|  
> format.html # show.html.erb  
> format.xml { render :xml =\> @devices }  
> end  
> end
> 
> The problem here is when accessing @devices\_array in the view  
> @devices\_array is nil. I _know_ the code is being executed in  
> initialize\_devices because if I don’t redirect to show\_all\_devices I can  
> manipulate the @devices\_array within the initialize\_devices view.  
> However, accessing AllDevicesController.all\_devices in show\_all\_devices  
> results in nil.
> 
> Anyone know why?

Further

@t = AllDevicesController.instance\_variable\_get(:@all\_devices)

Allows me to retrieve the all\_devices var in initialize\_devices but NOT  
in the show\_all\_devices portion of the controller. Instead, it is nil.

---

<div class="post-metadata">

**Author:** ![knappe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5fc32e/32.png) [@knappe](https://www.ruby-forum.com/u/knappe)\
**Post date:** [July 23, 2009, 8:57pm UTC](https://www.ruby-forum.com/t/singleton-object-nil/172528/4 "2009-07-23T20:57:49Z")

</div>

Frederick C. wrote:

> On Jul 22, 10:05ï¿½pm, Tyler K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > The problem here is when accessing @devices\_array in the view  
> > @devices\_array is nil. ï¿½I _know_ the code is being executed in  
> > initialize\_devices because if I don’t redirect to show\_all\_devices I can  
> > manipulate the @devices\_array within the initialize\_devices view.  
> > However, accessing AllDevicesController.all\_devices in show\_all\_devices  
> > results in nil.
> > 
> > Anyone know why?
> 
> because in development mode your source code is reloaded between  
> requests (ie it is no longer the same AllDevicesController class).
> 
> Fred

This was exactly the problem I was experiencing.

Thanks Fred!
