Rails always using production environment - ruby-on-rails

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.

Related

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.

config.ru file for Rails 2.3.18 app

Does anyone know what the contents of config.ru should be for a Rails 2.3.18 app in production to run on Passenger/Unicorn/Puma?
So far I've got:
# Require your environment file to bootstrap Rails
require ::File.dirname(__FILE__) + '/config/environment'
# Dispatch the request
run ActionController::Dispatcher.new
but it's loading development instead of the correct production environment.
It turns out this is a perfect config.ru.
The real problem is that Unicorn's -E parameter sets RACK_ENV and Rails 2.3.18 needs RAILS_ENV in order to correctly detect the environment.
So, at the top of config/environment.rb, I've set ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] and this is working just great.

Rails console default environment

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.

Change a Rails application to production

How can I change my Rails application to run in production mode? Is there a config file, environment.rb for example, to do that?
This would now be
rails server -e production
Or, more compact
rails s -e production
It works for rails 3+ projects.
How to setup and run a Rails 4 app in Production mode (step-by-step) using Apache and Phusion Passenger:
Normally you would be able to enter your Rails project, rails s, and get a development version of your app at http://something.com:3000. Production mode is a little trickier to configure.
I've been messing around with this for a while, so I figured I'd write this up for the newbies (such as myself). There are a few little tweaks which are spread throughout the internet and figured this might be easier.
Refer to this guide for core setup of the server (CentOS 6, but it should apply to nearly all Linux flavors): https://www.digitalocean.com/community/tutorials/how-to-setup-a-rails-4-app-with-apache-and-passenger-on-centos-6
Make absolute certain that after Passenger is set up you've edited the /etc/httpd/conf/httpd.conf file to reflect your directory structure. You want to point DocumentRoot to your Rails project /public folder Anywhere in the httpd.conf file that has this sort of dir: /var/www/html/your_application/public needs to be updated or everything will get very frustrating. I cannot stress this enough.
Reboot the server (or Apache at the very least - service httpd restart )
Enter your Rails project folder /var/www/html/your_application and start the migration with rake db:migrate. Make certain that a database table exists, even if you plan on adding tables later (this is also part of step 1).
RAILS_ENV=production rake secret - this will create a secret_key that you can add to config/secrets.yml . You can copy/paste this into config/secrets.yml for the sake of getting things running, although I'd recommend you don't do this. Personally, I do this step to make sure everything else is working, then change it back and source it later.
RAILS_ENV=production rake db:migrate
RAILS_ENV=production rake assets:precompile if you are serving static assets. This will push js, css, image files into the /public folder.
RAILS_ENV=production rails s
At this point your app should be available at http://something.com/whatever instead of :3000. If not, passenger-memory-stats and see if there an entry like 908 469.7 MB 90.9 MB Passenger RackApp: /var/www/html/projectname
I've probably missed something heinous, but this has worked for me in the past.
If you're running on Passenger, then the default is to run in production, in your apache conf:
<VirtualHost *:80>
ServerName application_name.rails.local
DocumentRoot "/Users/rails/application_name/public"
RailsEnv production ## This is the default
</VirtualHost>
If you're just running a local server with mongrel or webrick, you can do:
./script/server -e production
or in bash:
RAILS_ENV=production ./script/server
actually overriding the RAILS_ENV constant in the enviornment.rb should probably be your last resort, as it's probably not going to stay set (see another answer I gave on that)
If mipadi's suggestion doesn't work, add this to config/environment.rb
# force Rails into production mode when
# you don't control web/app server and can't set it the proper way
ENV['RAILS_ENV'] ||= 'production'
Change the environment variable RAILS_ENV to production.
$> export RAILS_ENV=production
You can also pass the environment to script/server:
$ script/server -e production
rails s -e production
This will run the server with RAILS_ENV = 'production'.
Apart from this you have to set the assets path in production.rb
config.serve_static_assets = true
Without this your assets will not be loaded.
RAILS_ENV=production rails s
OR
rails s -e production
By default environment is developement.
As others have posted: rails server -e production
Or, my personal fave: RAILS_ENV=production rails s
In Rails 3
Adding Rails.env = ActiveSupport::StringInquirer.new('production') into the application.rb and rails s will work same as rails server -e production
module BlacklistAdmin
class Application < Rails::Application
config.encoding = "utf-8"
Rails.env = ActiveSupport::StringInquirer.new('production')
config.filter_parameters += [:password]
end
end
It is not a good way to run rails server in production environment by "rails server -e production", because then rails runs as a single-threaded application, and can only respond to one HTTP request at a time.
The best article about production environment for rails is Production Environments - Rails 3
for default server : rails s -e production
for costum server port : rails s -p [port] -e production, eg. rails s -p 3002 -e production
By default server runs on development environment: $ rails s
If you're running on production environment: $ rails s -e production or $ RAILS_ENV=production rails s
Please make sure you have done below in your environment.rb file.
ENV['RAILS_ENV'] ||= 'production'
If you application runs in shared hosting environment or phushion passenger, you might need to need make changes in .httaccess (inside public folder) and set mode as production.

How to run Rails console in the test environment and load test_helper.rb?

The background: I'm having some problems with Thoughtbot's "Factory Girl" gem, with is used to create objects to use in unit and other tests. I'd like to go to the console and run different Factory Girl calls to check out what's happening. For example, I'd like to go in there are do...
>> Factory(:user).inspect
I know that you can run the console in different environments...
$ script/console RAILS_ENV=test
But when I do that, Factory class is not available. It looks as though test_helper.rb is not getting loaded.
I tried various require calls including one with the absolute path to test_helper.rb but they fail similarly to this:
$ script/console RAILS_ENV=test
>> require '/Users/ethan/project/contactdb/test/test_helper.rb'
Errno::ENOENT: No such file or directory -
/Users/ethan/project/contactdb/config/environments/RAILS_ENV=test.rb
Grr. Argh.
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.
In Rails 3, just do rails console test or rails console production or rails console development (which is the default).
For Rails 5.2.0: "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."
rails c -e test
script/console test
Should be all you need.
You can specify the environment in which the console command should operate.
rails c [environment]
Examples
1) For Staging
rails c staging
2) For Production
rails c production
For source & detailed description: The Rails Command Line
David Smith is correct, just do
script/console test
The help command will show why this works:
$ script/console -h
Usage: console [environment] [options]
-s, --sandbox Rollback database modifications on exit.
--irb=[irb] Invoke a different irb.
--debugger Enable ruby-debugging for the console.
It's the [environment] bit.
I share the asker's pain. There are really three separate questions here, some of which are addressed, some not:
How do you start the console in the test environment?
For recent Rails versions, bundle exec rails c test, or alternative syntaxes for that.
How do you ensure that test/test_helper.rb is loaded into that console session?
Something like require './test/test_helper' ought to do it.
For me, this returns true, indicating that it was not already loaded when I started the console. If that statement returns false, then you just wasted a few keystrokes, but you're still good to go.
Once test_helper is loaded, how do you call the methods defined in it?
In a typical test_helper, the custom methods are typically defined as instance methods of ActiveSupport::TestCase. So if you want to call one of them, you need an instance of that class. By trial and error, ActiveSupport::TestCase.new has one required parameter, so...pass it something.
If your test_helper has a method called create_user, you could invoke it this way:
ActiveSupport::TestCase.new("no idea what this is for").create_user
Make sure you installed the GEM and you added the following line either in your environment.rb or test.rb file.
config.gem "thoughtbot-factory_girl", :lib => "factory_girl", :source => "http://gems.github.com"
Test Env
rails console test # or just rails c test
Developement Env
rails console # or just rails c
Command to run rails console test environment is
rails c -e test
or
RAILS_ENV=test rails c
if you are facing problem something like
ActiveRecord::StatementInvalid:
Mysql2::Error: Table 'DB_test.users' doesn't exist: SHOW FULL FIELDS FROM `users`
then you should first prepare your test DB by running
bundle exec rake db:test:prepare

Resources