# Class variable not retaining its old val after server start

**URL:** <https://www.ruby-forum.com/t/class-variable-not-retaining-its-old-val-after-server-start/124275>\
**Category:** Rails\
**Created:** [December 27, 2007, 9:02am UTC](https://www.ruby-forum.com/t/class-variable-not-retaining-its-old-val-after-server-start/124275 "2007-12-27T09:02:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vidya1111](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b3f665/32.png) [@vidya1111](https://www.ruby-forum.com/u/vidya1111)\
**Post date:** [December 27, 2007, 9:02am UTC](https://www.ruby-forum.com/t/class-variable-not-retaining-its-old-val-after-server-start/124275/1 "2007-12-27T09:02:09Z")

</div>

I want to get the names of the subclasses.

In my model class, I have  
cattr\_accessor :subtypes

def self.inherited(sub)  
super  
@@subtypes ||= []  
@@subtypes.push(sub.name)  
session[:subtypes] = @@subtypes  
end

@subtypes has names of the subclasses in array, but give nil value when  
accessed outside this method since the variable is initialized (in  
development mode), everytime the classes are reloaded.

So I tried putting it in session, but am getting an exception as  
“…undefined local variable or method `session’ for Sample:Class  
(NameError)” in my Server startup.

Any other suggestions other than session logic?

Appreciate any help.

---

<div class="post-metadata">

**Author:** ![vidya1111](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b3f665/32.png) [@vidya1111](https://www.ruby-forum.com/u/vidya1111)\
**Post date:** [December 27, 2007, 9:57am UTC](https://www.ruby-forum.com/t/class-variable-not-retaining-its-old-val-after-server-start/124275/2 "2007-12-27T09:57:47Z")

</div>

On 27 Dec 2007, at 08:02, Vidya Ramachandren wrote:

> session[:subtypes] = @@subtypes  
> end
> 
> @subtypes has names of the subclasses in array, but give nil value  
> when  
> accessed outside this method since the variable is initialized (in  
> development mode), everytime the classes are reloaded.

> So I tried putting it in session, but am getting an exception as  
> “…undefined local variable or method `session’ for Sample:Class  
> (NameError)” in my Server startup.

The session doesn’t exist in a model.  
The other problem you will may have is that if your class A has  
subclasses B,C,D but for some reason only B & C have been used at some  
point then your @@subtypes method will only return [B,C] since D won’t  
have been loaded.

Fred

---

<div class="post-metadata">

**Author:** ![vidya1111](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b3f665/32.png) [@vidya1111](https://www.ruby-forum.com/u/vidya1111)\
**Post date:** [December 27, 2007, 10:37am UTC](https://www.ruby-forum.com/t/class-variable-not-retaining-its-old-val-after-server-start/124275/3 "2007-12-27T10:37:32Z")

</div>

Frederick C. wrote:

> On 27 Dec 2007, at 08:02, Vidya Ramachandren wrote:
> 
> > session[:subtypes] = @@subtypes  
> > end
> > 
> > @subtypes has names of the subclasses in array, but give nil value  
> > when  
> > accessed outside this method since the variable is initialized (in  
> > development mode), everytime the classes are reloaded.
> 
> > So I tried putting it in session, but am getting an exception as  
> > “…undefined local variable or method `session’ for Sample:Class  
> > (NameError)” in my Server startup.
> 
> The session doesn’t exist in a model.  
> The other problem you will may have is that if your class A has  
> subclasses B,C,D but for some reason only B & C have been used at some  
> point then your @@subtypes method will only return [B,C] since D won’t  
> have been loaded.
> 
> Fred

What do you think should be done for this issue?  
Works in production mode…

~Thanks

---

<div class="post-metadata">

**Author:** ![vidya1111](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b3f665/32.png) [@vidya1111](https://www.ruby-forum.com/u/vidya1111)\
**Post date:** [December 27, 2007, 11:43pm UTC](https://www.ruby-forum.com/t/class-variable-not-retaining-its-old-val-after-server-start/124275/4 "2007-12-27T23:43:15Z")

</div>

What are you trying to accomplish with this anyway?

–  
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:** ![vidya1111](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b3f665/32.png) [@vidya1111](https://www.ruby-forum.com/u/vidya1111)\
**Post date:** [December 28, 2007, 4:01am UTC](https://www.ruby-forum.com/t/class-variable-not-retaining-its-old-val-after-server-start/124275/5 "2007-12-28T04:01:17Z")

</div>

On Dec 27, 12:02 am, Vidya Ramachandren \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> I want to get the names of the subclasses.

Yeah, forgive me if you’re aware of all this, but making a class aware  
of its descendants is a violation of OOP principles, especially if  
there’s any conditional logic involved. Classes should take care of  
themselves, if only to encapsulate the code so you can make changes  
and track down bugs easier. I know it’s sometimes necessary to break  
the rules, but if you wouldn’t mind, I’d be interested too in what the  
goal is here.

///ark

---

<div class="post-metadata">

**Author:** ![vidya1111](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b3f665/32.png) [@vidya1111](https://www.ruby-forum.com/u/vidya1111)\
**Post date:** [December 28, 2007, 5:26am UTC](https://www.ruby-forum.com/t/class-variable-not-retaining-its-old-val-after-server-start/124275/6 "2007-12-28T05:26:42Z")

</div>

I want to display the names of subclasses in my select dropdown.

Mark W. wrote:

> On Dec 27, 12:02ï¿½am, Vidya Ramachandren \<rails-mailing-l…@andreas-  
> [s.net](http://s.net)\> wrote:
> 
> > I want to get the names of the subclasses.
> 
> Yeah, forgive me if you’re aware of all this, but making a class aware  
> of its descendants is a violation of OOP principles, especially if  
> there’s any conditional logic involved. Classes should take care of  
> themselves, if only to encapsulate the code so you can make changes  
> and track down bugs easier. I know it’s sometimes necessary to break  
> the rules, but if you wouldn’t mind, I’d be interested too in what the  
> goal is here.
> 
> ///ark

---

<div class="post-metadata">

**Author:** ![vidya1111](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b3f665/32.png) [@vidya1111](https://www.ruby-forum.com/u/vidya1111)\
**Post date:** [December 28, 2007, 6:00am UTC](https://www.ruby-forum.com/t/class-variable-not-retaining-its-old-val-after-server-start/124275/7 "2007-12-28T06:00:00Z")

</div>

We do a similar thing here. The only way we figured around it was to  
declare  
our subclasses in a constant in the config/environment.rb file. That way  
we  
could access it all the time. The downside is that when we create a new  
subclass (which hasn’t happened since the very early stages of  
development,  
and I don’t forsee happening any time soon), we just have to add it to  
that  
list.
