I was wondering if there is a way to list all defined environments in a Rails application.
For instance if an application has 4 defined environments (production, staging, development, test) I would like to have obtain the following array
["production", "staging", "development", "test"]
Any ideas? Thanks
When we generate a new Rails application we get three environments by default: development , test and production . There's nothing special about these particular environments, though, and there are very few places in the Rails source code that refer to them.
By default rails has 3 environments, development , production and test . By editing each file you are editing the configuration for that environment only. Rails also has a configuration file in config/application.
You cannot switch environments once Rails has been loaded in a process (for server, console, tests, rake tasks,...). You need to specify the environment when launching the process, and cannot change it afterwards. Stop the process, and start again with another environment if you need it.
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.
I'm not sure if you can get the list of defined environments through some Rails API. Environment files are loaded dynamically based on the current environment. So as already mentioned, you can just glob the config/environments
directory for any .rb
file.
Dir.glob("./config/environments/*.rb").map { |filename| File.basename(filename, ".rb") }
If you want to get a list of all database
environments defined in database.yml
, you can get the list from:
ActiveRecord::Base.configurations.to_h.keys
Assuming you are actually using AR.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With