70

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.

ChrisInEdmonton
  • 4,470
  • 5
  • 33
  • 48

2 Answers2

112

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
molf
  • 73,644
  • 13
  • 135
  • 118
  • Heh. Knew it would be something obvious like that. I tried Rails.config, didn't think to try that. Thanks. I'll be accepting this answer once the time limit expires. – ChrisInEdmonton Nov 30 '10 at 23:35
  • There is an exception with perform_caching, which can be set per controller too, so you should access it per controller – iain Nov 30 '10 at 23:37
  • 8
    For me it was `MyApp::Application.config.foo` (Rails 4.1). – Andrey Mikhaylov - lolmaus Nov 18 '14 at 11:20
  • From what I can see, they are identical [at least](https://github.com/rails/rails/commit/e749424dfa38a0300a621b772eae96f9cc5d2555#diff-a79c10f572ebe2d487fd1ec69eabf45a2cd0e011c7dd74555da695a6f222ae98R50) since [Rails 3.0.20](https://github.com/rails/rails/commit/5f34bd61b0a49478ddf6a9a69654b8e56f0f20d3#diff-a79c10f572ebe2d487fd1ec69eabf45a2cd0e011c7dd74555da695a6f222ae98R46). – x-yuri Aug 20 '21 at 14:16
4

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

::Rails.application.config

kross
  • 3,627
  • 2
  • 32
  • 60