I have an ActiveRecord model with a cattr_accessor. The class attribute is set up like this: class MyModel < ActiveRecord::Base cattr_accessor :my_attribute end Because I need to give #my_attribute environment-specific values, I try to set this attribute in environments/development.rb like so: MyModel.my_attribute = 3 But this leads to odd and erratic behavior when #my_attribute is called from a controller. Sometimes MyModel.my_attribute returns 3, but sometimes it returns nil. Any suggestions? --~--~---------~--~----~------------~-------~--~----~ 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 Reginald James
2008-Dec-31 01:01 UTC
Re: when to set a class attribute variable during boot
Daniel Choi wrote:> I have an ActiveRecord model with a cattr_accessor. The class > attribute is set up like this: > > class MyModel < ActiveRecord::Base > cattr_accessor :my_attribute > end > > Because I need to give #my_attribute environment-specific values, I > try to set this attribute in environments/development.rb like so: > > MyModel.my_attribute = 3 > > But this leads to odd and erratic behavior when #my_attribute is > called from a controller. Sometimes MyModel.my_attribute returns 3, > but sometimes it returns nil.In development mode, models get reloaded on every request, so your variable won''t persist. You''ll have to set it in in a persistent class or module, in the global context, or in the database or session. -- Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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 -~----------~----~----~----~------~----~------~--~---