38

I'd like to start the rails console and create database entries in a database that isn't the default database, such as the testing database. I'd appreciate any help.

demongolem
  • 9,474
  • 36
  • 90
  • 105
James
  • 5,273
  • 10
  • 51
  • 76

4 Answers4

63

To start console in test environment:

ruby script/console test 
ruby script/console production 

To run rake tasks in test environment:

rake db:create RAILS_ENV=test 
rake db:migrate RAILS_ENV=test 

In Rails 3 and 4 you can use:

rails console test
rails c test

In Rails 5 you can use:

rails console -e test
rails c -e test
Chiperific
  • 4,428
  • 3
  • 21
  • 41
Harish Shetty
  • 64,083
  • 21
  • 152
  • 198
  • 5
    For completeness, `rails console -e test` or `rails c -e test` also works (in Rails 5.2 https://stackoverflow.com/a/53127882/188574) > Passing the environment's name as a regular argument is deprecated and will be removed in the next Rails version. Please, use the -e option instead. – Tun Feb 08 '19 at 08:04
10

You can pass the environment as RAILS_ENV=test:

$ RAILS_ENV=test bundle exec rails console

Or:

$ RAILS_ENV=test bundle exec rails c

You can also do:

$ bundle exec rails console test

Or:

$ bundle exec rails c test

You can see like

Loading test environment (Rails 3.2.8)
1.9.3p327 :001 >
Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
Jyothu
  • 3,104
  • 17
  • 26
8

in Rails 6 the syntax has changed for this

rails c -e test
Toby 1 Kenobi
  • 4,717
  • 2
  • 29
  • 43
0
$ RAILS_ENV=test ./script/console
MBO
  • 30,379
  • 5
  • 50
  • 52