# class variable not retaining its old val after server start

**URL:** <https://discuss.rubyonrails.org/t/class-variable-not-retaining-its-old-val-after-server-start/22284>\
**Category:** rubyonrails-talk\
**Created:** [December 27, 2007, 8:56am UTC](https://discuss.rubyonrails.org/t/class-variable-not-retaining-its-old-val-after-server-start/22284 "2007-12-27T08:56:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [December 27, 2007, 8:56am UTC](https://discuss.rubyonrails.org/t/class-variable-not-retaining-its-old-val-after-server-start/22284/1 "2007-12-27T08:56:54Z")

</div>

> I want to get the names of the subclasses.
> 
> In my model class, I have cattr\_accessor :subtypes
> 
> def self.inherited(sub) &nbsp;&nbsp;&nbsp;super &nbsp;&nbsp;&nbsp;@@subtypes ||= &nbsp;&nbsp;&nbsp;@@subtypes.push(sub.name) &nbsp;&nbsp;&nbsp;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
