Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access a Rails configuration value during runtime?

I'm using Rails 2.3.x. I would like a small section of code to run if and only if the config.cache_classes is true. By default, that's true for production and false for development.

How do I access the value of config.cache_classes from outside of my environment.rb, development.rb, and production.rb files? It's easy to tell if we are in production or development, Rails.env will give us the answer. But there's no guarantee the developer hasn't set config.cache_classes = true in development.

I certainly understand that you do not generally want to run separate code paths in development and production. In this particular instance, we are simply not performing some work on startup; if we need to perform it later, we will do so, both in development and production.

like image 475
ChrisInEdmonton Avatar asked Nov 30 '10 23:11

ChrisInEdmonton


People also ask

What are Initializers in Rails?

An initializer is any file of ruby code stored under /config/initializers in your application. You can use initializers to hold configuration settings that should be made after all of the frameworks and plugins are loaded.

What is config in Rails?

In general, the work of configuring Rails means configuring the components of Rails, as well as configuring Rails itself. The configuration file config/application. rb and environment-specific configuration files (such as config/environments/production.

Where is database Yml in Rails?

yml file in /shared/config directly on the server.

Where is Rails application config?

You probably know that you can configure Rails in config/application. rb and config/environments/development. rb etc. But you can also leverage that for configuring your own custom settings for your application.


1 Answers

For Rails 2, you can do:

Rails.configuration.cache_classes 

If you ever switch to Rails 3, it'll be different; you can access the same value with:

Rails.application.config.cache_classes 
like image 100
molf Avatar answered Oct 04 '22 00:10

molf