I have a class variable that I would like to set from an initilizer and have the value kept from then on. The example below works for only the first page load. Is there a better way to do this?
app/models/token.rb
class Token
class << self
attr_accessor :salt
end
end
config/initilizers/token.rb
Token.salt = "savory hash"
In development mode, your class is going to get reloaded with every request, so the value that’s set in an initializer at app startup will not persist when the class is reloaded after the first request. (The result of “config.cache_classes = false” in your development.rb).
However, if you want to set a value in an initializer and have it persist in development mode, you can either add it as a constant:
initializers.rb
OR as an application config variable:
application.rb
which would be accessible anywhere in the app with:
Of course, if you enable class caching in your environment, you should find that your variable’s value will persist without doing anything of the above.