stackjlei
stackjlei

Reputation: 10035

Where is the RAILS_ENV?

I'm looking at this article about rake commands but don't where the RAILS_ENV is. Is that in a specific file somewhere?

db:create Creates the database for the current RAILS_ENV environment. If RAILS_ENV is not specified it defaults to the development and test databases.

Upvotes: 4

Views: 22209

Answers (5)

hiveer
hiveer

Reputation: 749

RAILS_ENV this is just the name of an environment variable. So it could be used when you run any rake tasks or rails command. Like:

RAILS_ENV=test rails c

But you will wonder how this env variable be used in Rails, Here is the place: https://github.com/rails/rails/blob/ce4d467f7c2fc812e257a87bd4875c1f1f08a981/railties/lib/rails.rb#L72

That means Rails will prefer to use RAILS_ENV to decide the current env.

Upvotes: 1

max
max

Reputation: 102036

RAILS_ENV is just an environmental variable which is set in the shell or the operating system itself (or when invoking the process).

Environment variables are a set of dynamic named values that can affect the way running processes will behave on a computer.
They are part of the environment in which a process runs. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME or USERPROFILE variable to find the directory structure owned by the user running the process.
https://en.wikipedia.org/wiki/Environment_variable

ENV["RAILS_ENV"] simply contains a string such as "production", "development" or "test". This tells Rails which configuration file in /config/environments to load - and which hash key in database.yml to use for the database.

For example if ENV["RAILS_ENV"] == "foo" Rails will:

  • load /config/environments/foo.rb
  • look for the key foo in database.yml.
  • Rails.env.foo? will be true.

The Rails concept of environment is thus somewhat different than the general computing concept - a Rails environment is a broader term for a set of settings and a database that serve different purposes such as automated testing or production.

See also:

Upvotes: 16

puneet18
puneet18

Reputation: 4427

RAILS_ENV is rails environment ie development, production or staging.

You can run commands for different environments using RAILS_ENV like:

RAILS_ENV=production rake db:migrate #runs migration in production env

RAILS_ENV=test rake db:migrate #runs migration in test env

By default environment is development.

Upvotes: 0

user3905264
user3905264

Reputation: 7

To my understanding, this "RAILS_ENV" seems much like environment variable which might be OS level (say under *uix environment, using env to check its value) or maybe it specific to the run-time which might get set in configuration file (.propertie or else).

Upvotes: -1

Deepak Mahakale
Deepak Mahakale

Reputation: 23671

It's the environment variable you can set by passing it before the command you are executing

RAILS_ENV=production rake db:migrate

You can also set some other ENV variables by passing them before the command

MAIL_USERNAME=user MAIL_PASSWORD=password rails server

Or you can export them to shell

export MAIL_USERNAME=user
export MAIL_PASSWORD=password
rails server

There is also a gem called dotenv which help you set env variables easily by saving them in a .env file which will not be pushed over git or whatever SVN you use

Upvotes: 2

Related Questions