My question is seemingly simple but has been pretty elusive. I want one value that can be called anywhere in the application – it’s a minimum value we are using for validations in the model, we want to inform the user of that value, etc.
So far, I have done the following:
In config/initializers, I created a new .rb file (under suggestion I found on another thread):
minimum = 15
and only got that it couldn’t be found after starting rails server.
I found a strange suggestion to put the value in en.yml as minimum: 15, same issue as above.
Obviously, application helper and controller won’t help as I need the value in my models. I did however find one solution, which was to put it in the class Application in config/application.rb:
config.after_initialize do
::Minimum = 15
end
and afterwards call it like this:
MyApp::Application::Minimum
Obviously, this solution, while it does work, isn’t ideal. If I could find a way to alias MyApp::Application::Minimum as just “minimum”, I’ll be completely satisfied, but getting it done in the initializers is, from what I’ve heard, a better solution.
You can stick something in a model if you want and call it from anywhere:
But I prefer putting them in a yaml file.
Create a config.yml file in your configs directory and put your vars in there:
Load the config file using something like this in your initializers directory. Create a file in /config/initializers called ‘load_vars.rb’ and put the following in there:
You’ll have to restart your server after changing these though.
— EDIT —
To use these variables, you can load with something like this from your model, controller, views: