0

I have an application that use two databases. Now I have in development environment. How can I set the databases for each environment by default in Rails? My database.yml looks like:

database1_dev:
  adapter: mysql2
  host: localhost
  database: db1
  user: root
  password: asdf
  pool: 5
  timeout: 5000

database2_dev:
  adapter: mysql2
  host: localhost
  database: db2
  user: root
  password: asdf
  pool: 5
  timeout: 5000
Cristhian Boujon
  • 4,060
  • 13
  • 51
  • 90
  • What do you mean by "application that uses two databases"? Does it need to connect to both the databases at the same time to function? – Litmus Oct 28 '13 at 15:16

1 Answers1

0

You need to setup a separate environment. By default in a new project rails sets up test, production and development. You need to setup development_2 or staging or whatever adequately describes what you're doing.

In your rails project in config/environments copy the development file to a new file with the name you're using for your environment.

Then in your database.yml file, make sure there is an entry named to match each environment. So if you have development and development_2 then your entries should be:

development:
  adapter: mysql2
  host: localhost
  database: db1 
  user: root
  password: asdf
  pool: 5
  timeout: 5000

development_2:
  adapter: mysql2
  host: localhost
  database: db2 
  user: root
  password: asdf
  pool: 5
  timeout: 5000

By default when you run a rails application it will be in development, so that is already selecting the development database from your yml file.

Now when you want to run in an alternative environment you'll just tell rails the env is development_2, if you're using the rails server command it will instead be

rails server -e development_2

Note that this new environment might break certain gems which are hard carded to work for only staging, test, production and development.

trh
  • 7,186
  • 2
  • 29
  • 41