running Rails console in production - ruby-on-rails

I have just gone live with my first Rails site, but now I have a problem. When I run the project in development mode on my IDE I can run the console to something like:
User.first.name='whatever' to change a users name.
How do I accomplish the same task on a live site in production mode?

if you're running rails 3.0 or greater, you can also use
rails console production
production can of course be substituted with development or test (value is development by default)
Adding the option --sandbox makes it so that any changes you make to your database in the console will be rolled back after you exit
If this isn't working for you, you may need to try
bundle exec rails console production
If you are actually trying to run the rails console on your production server, try googling "run rails console [your cloud hosting provider]" e.g. "run rails console heroku"
As of Rails 6 you need to use
RAILS_ENV=production bundle exec rails c
or
RAILS_ENV=production rails c
depending on your setup

Pretty easy:
RAILS_ENV=production rails console

If you have already deployed your site to the server, you can also use:
bundle exec rails console production
...in the webroot of your rails app. That is if you haven't installed the rails package directly on the server yet or if you want to run console within the context of your web app.

Try below command.
rails c -e production

Note: This answer assumes you are using Heroku as your hosting service.
It depends on what hosting service you are using. For Heroku, you can go to your terminal and type in
heroku run rails console
This will load up the rails console for your production site and will allow you to create records for your live site.
You can also look into seeding a database but that is generally meant for testing. RailsCasts has some videos on the topic but they are a bit outdated.

With Rails 6.1.6 on AlmaLinux8, the below command worked for me.
bundle exec rails console -e production

today with rails 6 run in console RAILS_ENV=production rails console

Related

Unable to run rails console/server in AWS Cloud9 CodeStar EB dev Ruby on Rails environment

I created my first environment with CodeStar and selected the Ruby on Rails w/ Elastic Beanstalk option. I'm using AWS Cloud9 for the IDE. I'd like to use the Preview option to view the impact of code changes prior to committing, and have looked through the docs at http://docs.aws.amazon.com/cloud9/latest/user-guide/app-preview.html, however I can't seem to get a server running in the development environment.
From within my environment directory in the Cloud9 terminal (path: /home/ec2-user/environment/env_name) I tried rails s -b $IP -p $PORT as documented for the previous non-AWS Cloud9, and also rails server and even rails console just to check. In each case I just get the help details for rails new:
$ rails s
Usage:
rails new APP_PATH [options]
Options:
-r, [--ruby=PATH] # Path to the Ruby binary of your choice
...etc...
What am I missing?
Per the discussion on this question, this behavior indicates that rails does not recognize that it is running in a rails directory so it thinks the only valid action is rails new. There were several suggested answers, but the one that worked for me was to run rake rails:update:bin (or rake app:update:bin for Rails 5).

Run Ruby on Rails migrations on specific Google App Engine service

I've been working on a Ruby on Rails API and I'm using Google Cloud Platform, specifically Google App Engine, to deploy my app. I followed the instructions here. So far so good. My app was successfully deployed and I could successfully run the migrations. Let's call this environment production.
The thing is I need to deploy a sort of staging environment. For the moment I had to create another project to solve my problem and consider that as my staging environment. Now, I don't think this is necessary, actually seems to be redundant.
I've prepared another database my_app_db_staging. And I created a staging.yaml file stating to run as another instance with the service name staging. The service was successfully deployed. Where is the problem? Well, running the migration. I'm using the appengine gem. So, to run the migrations you're supposed to run:
bundle exec rake appengine:exec -- bundle exec rake db:migrate
But as the documentation states:
The appengine gem provides the Rake task appengine:exec to run a command against the most recent deployed version of your app in the production App Engine environment.
So, no clue on how to run the migrations against my recently created staging service.
I looked deeper into the appengine gem, and it has an option. It lacks more documentation so you don't have to waste time looking for an answer:
rake appengine:exec GAE_CONFIG=staging.yaml -- bundle exec rake db:migrate
The options can work together with GAE_SERVICE, but the yaml file has to have the same name, otherwise you'll get an error.

rails console, local vs something like heroku

So I know I can do things like run rails console and change user values in my local environment. How does this apply to something like my rails app when deployed to heroku? Can I use the rails console there?
Of course you can, with Heroku Toobelt you can run heroku run rails console, more here: https://devcenter.heroku.com/articles/getting-started-with-rails4#console

Loading rails env in production

I have deployed a new Rails site to a Linux VM using Capistrano. I am using nginx as the front end and running my Rails app using unicorn.
If I try to run rake routes on the server, I get an error telling me that Rails is not installed, even though Rails is installed. The problem seems to be that the gem search directory is different for the app and the logged in user.
How do I load the Rails environment that my app is seeing as the logged in user?
Just use:
RAILS_ENV=production bundle exec rake routes
The RAILS_ENV part sets your environment variable so your app is loaded in full production mode, including database settings and so on.
The bundle exec part is necessary so that any commands that come after that are executed within the environment of the gems installed in your Gemfile.

Welcome aboard ActiveRecord::ConnectionNotEstablished

I'm using Ubuntu, with Rails 3.0.1 with mysql2 socket.
When i do runing install, rake db:create and after rails server, my Welcome aboard, shows ActiveRecord::ConnectionNotEstablished in About your application’s environment
What i do?
Had the same problem on rails 3.1.1:
rake db:create - ok
rails console and some DMLs - ok
but accessing info from the web-page resulted in ActiveRecord::ConnectionNotEstablished.
A rails server restart helped.
It sounds like your MySQL server isn't running. You'll need to install MySQL if you haven't already (apt-get install mysql-server should do it). Once it's running, you'll need to set up a user and database for your app, and note the username and password so that you can put that information in config/database.yml within your app.
This link will be useful if you need any help with those steps.
You'll need to do some more debugging to work it out.
How are you running your server?
Make yourself a model.
rails generate model Something name:string
Then try running rake db:migrate
Does this work?
If it does, then you must be running your server in a different way (maybe you're running it in production mode?)
Try rails console and try to do Something.count
If all of those work
then I'd suggest you try restarting your server.
rails server

Resources