How to access Rails configuration value at runtime?

I am using Rails 2.3.x. I would like a small part of the code to run if and only if the value of config.cache_classes true. By default, this is true for production and false for development.

How do I access the config.cache_classes value outside of my environment.rb, development.rb, and production.rb files? It is easy to say whether we are in production or development, Rails.env will give us the answer. But there is no guarantee that the developer did not set config.cache_classes = true in development.

Of course, I understand that you generally do not want to run separate code paths in development and production. In this particular case, we simply do not do some startup work; if we need to complete it later, we will do it, both in development and in production.

+56
ruby-on-rails
Nov 30 '10 at 23:28
source share
2 answers

For Rails 2 you can:

 Rails.configuration.cache_classes 

If you ever switch to Rails 3, it will be different; you can get the same value with:

 Rails.application.config.cache_classes 
+90
Nov 30 '10 at 23:33
source share

Depending on where you are in the module, you may need access to the root namespace. This should provide access from anywhere in a universal way for 3+ rails:

::Rails.application.config

+1
Feb 15 '16 at 19:39
source share



All Articles