Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list all defined environments in a Rails 3 application?

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

like image 438
Dorian Avatar asked Dec 26 '12 17:12

Dorian


People also ask

What are the environments does the Rails have by default?

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.

What are environments in Rails?

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.

How do I change environment in Ruby on Rails?

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.

What is Rails application config?

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.


1 Answers

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.

like image 139
Jiří Pospíšil Avatar answered Oct 25 '22 12:10

Jiří Pospíšil