Class variable not retaining its old val after server start

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.

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

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

What are you trying to accomplish with this anyway?


Ryan B.
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.

On Dec 27, 12:02 am, Vidya Ramachandren <rails-mailing-l…@andreas-
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

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> 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

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.