Rails console default environment
The rails executable can't know which environment should run on which machine.
you can put export RAILS_ENV=production
in your ~/.bashrc
or ~/.bash_profile
file of the user you want to start the console with.
How to run Rails console in the test environment and load test_helper.rb?
For Rails < 3.0
Run script/console --help
. You'll notice that the syntax is script/console [environment]
, which in your case is script/console test
.
I'm not sure if you have to require the test helper or if the test environment does that for you, but with that command you should at least be able to boot successfully into the test env.
As a sidenote: It is indeed kind of odd that the various binaries in script/ has different ways of setting the rails environment.
For Rails 3 and 4
Run rails c test
. Prepend bundle exec
if you need this for the current app environment.
For Rails 5 and 6
Run rails console -e test
.
Rails console not loading environment
This happens when spring is running in production. Try either:
spring stop
or look for running spring process using ps -eaf | grep spring
and kill them.
Try the console again once spring is stopped successfully.
How to manually set ENV variables when running rails console
From your console.
env RAILS_MASTER_KEY="..." rails c
See more here https://www.honeybadger.io/blog/ruby-guide-environment-variables/
How can I make a custom environment in rails a default environment?
Ideally you have to set environment variable in .bashrc
like
export RAILS_ENV=staging
because rails
is fully dependent on environment variable. But like you said
adding RAILS_ENV in ~/.bashrc or ~/.bash_profile file of the user. will make this application depent on the console, shouldn't it just work independent of ~/.bashrc or ~/.bash_profile file?
Obviously, this is another option. Include this line at the top of config/boot.rb
ENV["RACK_ENV"] = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "staging"
This will work everywhere. I have tested in following places
- Rails 4
- Rake
- Server
- Console
- dbconsole
- It will pick the environment if it is set in
bashrc
orzshrc
etc.
Rails console won't load environment variables in secrets.yml
TL;DR: spring stop
It turns out, as has happened so many times when things aren't making any sense, Spring is the culprit! I solved this problem (thanks to a related discussion) by running spring stop
and then trying again, after which it worked perfectly!
Apparently Spring was caching the environment, or certain pieces of the Rails application, and neglecting to reload them when the environment variables changed.
How to check rails environment?
One liner if you are in app root
rails r "puts Rails.env"
Related Topics
Weird Rails Error "Permission Denied: Bin/Rails" for Old Rails Apps
Adding a Submit Button Image to a Rails Form
Escape Double and Single Backslashes in a String in Ruby
What Happens When Modifying Gemfile.Lock Directly
How Would I Go About Programmatically Interacting with VSt(I) Plugins to Synthesize Audio
How to Render a Plain HTML File with Sinatra
How to Convert a JSON String to an Object
What Is the Best Wysiwyg for Rails - Ruby on Rails Blog
Using Gets() Gives "No Such File or Directory" Error When I Pass Arguments to My Script
How to Split a String into Only Two Parts, by the Last Occurrence of the Split Char
Circular Dependency Detected While Autoloading Constant When Loading Constant
No Such File to Load Bundler Error for Rails 3
How to Create Md5 Hash with Hmac Module in Ruby
Why Doesn't This Work If in Ruby Everything Is an Object
How to Safely Join Relative Url Segments