Forum: Ruby on Rails when to set a class attribute variable during boot

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
004e78a7e0aaf9f1eb0009668cd84b1a?d=identicon&s=25 Daniel Choi (dchoi)
on 2008-12-31 01:31
(Received via mailing list)
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?
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2008-12-31 02:02
(Received via mailing list)
Daniel Choi wrote:
> 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
This topic is locked and can not be replied to.