How to create a new environment in Ruby on Rails?

Ruby on-Rails

Ruby on-Rails Problem Overview


I'm trying to create a new environment (build) for using it with hudson.

I'm doing this because I didn't want to mix up my test databases with hudson test database.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Assuming you want create the hudson environment.

  1. Create a new environment file in config/environments/hudson.rb. You can start by cloning an existing one, for instance config/environments/test.rb.
  2. Add a new configuration block in config/database.yml for your environment.
  3. Update any other configuration file you might have under the config folder with your new environment, some gems create their own ymls under the config folder.
  4. That's all.

Now you can start the server

rails server -e hudson

or run the console

rails console hudson

And so on.

If you are using an older version of Rails, the commands are

ruby script/server -e hudson
ruby script/console hudson

Solution 2 - Ruby on-Rails

> Updated Answer for Rails 5

Create new environment file:

config/environments/staging.rb

Modify the following files to add the environment key 'staging'

config/cable.yml
config/database.yml
config/secrets.yml
Gemfile (incase you have stage dependent gems)

The new environments can now be used as usual for eg:

rails server -e staging

rails console staging

Or to do conditional checks:

rails console staging, Rails.env.staging?

Good place to start for creating new environment and to modify these files is to copy production settings.

Solution 3 - Ruby on-Rails

If you're using webpacker in your Rails 5 app, also make sure to update your config/webpacker.yml. If you forget to add your new environment to config/webpacker.yml, it will just fall back to using your production config.

Solution 4 - Ruby on-Rails

Create a file config/environments/build.rb which will contain the options specific to your environment.
Add your new environment's database credentials in config/database.yml.

Rock and roll!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZX12RView Question on Stackoverflow
Solution 1 - Ruby on-RailsSimone CarlettiView Answer on Stackoverflow
Solution 2 - Ruby on-RailsShaunakView Answer on Stackoverflow
Solution 3 - Ruby on-RailshidaceView Answer on Stackoverflow
Solution 4 - Ruby on-RailsDamien MATHIEUView Answer on Stackoverflow