Vidya Ramachandren
2007-Dec-27 08:02 UTC
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Dec-27 08:56 UTC
Re: class variable not retaining its old val after server start
On 27 Dec 2007, at 08:02, Vidya Ramachandren wrote:> > 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.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> > > Any other suggestions other than session logic? > > Appreciate any help. > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Vidya Ramachandren
2007-Dec-27 09:37 UTC
Re: class variable not retaining its old val after server st
Frederick Cheung 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. > > FredWhat do you think should be done for this issue? Works in production mode.. ~Thanks -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Bigg
2007-Dec-27 22:42 UTC
Re: class variable not retaining its old val after server start
What are you trying to accomplish with this anyway? -- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mark Wilden
2007-Dec-28 03:00 UTC
Re: class variable not retaining its old val after server start
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Vidya Ramachandren
2007-Dec-28 04:26 UTC
Re: class variable not retaining its old val after server st
I want to display the names of subclasses in my select dropdown. Mark Wilden 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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Bigg
2007-Dec-28 04:59 UTC
Re: class variable not retaining its old val after server st
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---