Michael Hartl mentions in his Rails Tutorial that Rails comes equipped with three environments: test
, development
, and production
.
Then an aside shows how to run a console, a server and a rake task in an environment different from the implicit development
because, as he confesses
I find it confusing that the console, server, and migrate commands specify non-default environments in three mutually incompatible ways, which is why I bothered showing all three.
Amongst the minds that cope better with consistent patterns, I also count mine. The trick that seems to save me some brain cycles is easy, almost too obvious. Declare the RAILS_ENV
environment variable right before the command, on the same line. In few cases this becomes slightly less elegant, but at least it's consistent.
For the sake of explicitness, here are the alternatives:
(note: the examples use the spring
ified executables)
the mutually incompatible way | the consistent pattern equivalent |
---|---|
bin/rails console test |
RAILS_ENV=test rails console |
bin/rails server --environment test |
RAILS_ENV=test rails server |
bin/rake db:migrate RAILS_ENV=test |
RAILS_ENV=test rake db:migrate |
Almost too obvious, you had been warned!
What other quirks do you leverage to save yourself extra brain cycles?
Cover image credit: https://500px.com/photo/105625329/matterhorn-by-%C4%B0lhan-eroglu