Rails console default environment - ruby-on-rails

On my development machine:
$ bundle exec rails console
Loading development environment (Rails 3.2.3)
1.9.3p194 :001 > Rails.env
=> "development"
This is expected. So far, so good.
Yet on my production server (to which I have deployed using Capistrano), I get exactly the same result:
$ bundle exec rails console
Loading development environment (Rails 3.2.3)
1.9.3p194 :001 > Rails.env
=> "development"
On either machine, I can instead do:
$ bundle exec rails console production
Loading development environment (Rails 3.2.3)
1.9.3p194 :001 > Rails.env
=> "production"
My question is: on the production server, shouldn't bundle exec rails console load the production environment by default, instead of the development environment? And if not, why not?

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.

RAILS_ENV is a variable like any other which will always default to development
if you like you can always open up '~/.bash_profile' on the production server and add this:
alias sc="bundle exec rails console production"
then run source ~/.bash_profile to reload that file for your terminal session and you can just call sc to load up console.

Related

Getting rails console to work with multiple Gemfiles

We are currently migrating from Rails 4 to 5, and have two Gemfiles (similar to how GitHub did it), Gemfile (Rails 4) and Gemfile_5 (Rails 5).
The following commands work as expected:
bundle exec rails s
=> Booting WEBrick
=> Rails 4.2.11.12 LTS application starting in development on http://localhost:3000
BUNDLE_GEMFILE=Gemfile_5 bundle exec rails s
=> Booting WEBrick
=> Rails 5.0.7.1 application starting in development on http://localhost:8000
bundle exec rspec spec/...
# runs specs using Rails 4 gemset
BUNDLE_GEMFILE=Gemfile_5 bundle exec rspec spec/...
# runs specs using Rails 5 gemset
bundle exec rails --version
Rails 4.2.11
BUNDLE_GEMFILE=Gemfile_5 bundle exec rails --version
Rails 5.0.7.1
And yet, when trying to run console or runner, it'll only use the Rails 4 gemset:
BUNDLE_GEMFILE=Gemfile_5 bundle exec rails c
Loading development environment (Rails 4.2.11.12 LTS)
BUNDLE_GEMFILE=Gemfile_5 bundle exec rails r "puts Rails.version"
4.2.11
I've tried restarting Spring, but that hasn't had any effect. What am I missing here?
The trick was to fully disable Spring, rather than just restart it:
DISABLE_SPRING=1 BUNDLE_GEMFILE=Gemfile_5 bundle exec rails console
Loading development environment (Rails 5.0.7.1)

Clockwork gem is trying to access development db in production

I am trying to start the clockwork with following command sudo bundle exec clockwork config/clock.rb in production. But it throws the following error
ActiveRecord::AdapterNotSpecified: 'development' database is not configured. Available: ["production"].
It works correctly in local. We have Puma server and JRuby setup in server.
Add the RAILS_ENV variable:
RAILS_ENV=production sudo bundle exec clockwork config/clock.rb
Or, set the RAILS_ENV variable in your .bashrc:
echo 'export RAILS_ENV=production' >> ~/.bashrc
And then exit the shell, and login again. From then on, clockwork (and all other rails related things) should be in production mode. You can use the command you were using originally.

rails console hangs in production environment

I have postgresql setup for my production schema in database.yml. I was able to successfully run RAILS_ENV=production rake db:migrate but when I do do RAILS_ENV=production rails console, it hangs. Never gives me the console prompt.
I've also tried bundle exec rails console production but didn't help.
Output:
Connecting to database specified by database.yml
...hangs after this.
Running rails 3.2.11 on ruby 2.1.2. I have rbenv and rbenv-gemsets installed.
To run the rails console in the production environment you need to pass the name of the environment after the command. The actual command is as follows.
rails console production
If that doesn't work, try restarting your PostgreSQL server and try again. It could be that you have crossed the number of possible connections with the PostgreSQL database.

Rails always using production environment

I'm having an issue where no matter what environment I try to run Rails in it always goes to production. For example:
$ rails c development
Loading production environment (Rails 3.2.16)
1.9.3p484 :001 >
$ RAILS_ENV=development rails console
Loading production environment (Rails 3.2.16)
1.9.3p484 :001 >
I first noticed this when I was running the Rails server and it was writing to the production database instead of development. If I run "rails s -e development" it says it starts up in development but still uses the production database.
Here's my config/environment.rb
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
Skeletor::Application.initialize!
I tried grepping through the project to see if RAILS_ENV was being set anywhere but I don't see it.
Try run:
RAILS_ENV=development bundle exec rails s
In an initializer I had done "if Rails.env = 'production'" instead of "if Rails.env == 'production'" which was causing the problem. Thanks for the suggestions everyone, I knew it had to be something silly.

Running Rails console on heroku, in sandbox mode

Couldn't find it in the docs, nor on SO, but is there a way to run the Rails (3.2.x) console in sandbox mode on heroku (Celadon Cedar), equivalent to
rails console --sandbox
For a more "the Heroku way" alternative, heroku run console --sandbox does the trick as well:
$ heroku run console --sandbox
Running `console --sandbox` attached to terminal... up, run.6024
[...]
Loading production environment in sandbox (Rails 3.2.12)
Any modifications you make will be rolled back on exit
irb(main):001:0>

Resources