rails console, local vs something like heroku - ruby-on-rails

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

Related

Rails/Heroku - calling ruby class method

I have a method in a class (Scraper.rb) that saves some data to the app's database. On my dev environment I just call it through the command line. How can I call it on an app that's hosted on Heroku?
If you want to run the method from heroku console as your dev env. you can run command
heroku run console
or
heroku run rails c
This would normally do the trick in my app:
heroku run rails runner Scrapper.rb -e production
Obviously my app is in production in heroku so you apply the environment you want.

rails console unable to connect to database in staging environment

Using capistrano I'm deploying my Rails 4 app under the environment name "staging". When I ssh onto the server and run rails console, any ActiveRecord queries I do come up with no such table. So I check my Rails.env and sure enough it reads development instead of staging. But even running rails console staging, it does set Rails.env to "staging", but still the ActiveRecord queries say no such table. The app itself is running fine (under Apache and Passenger), but for some reason the rails console is unable to connect to the db.
I've reverted back to when I knew it was working and am still seeing the error, which tells me it is a configuration change i must've made on the server as opposed to a change to the rails app code or capistrano deploy config.
OK, tracked down the answer myself. The RAILS_ENV was originally being set to staging in /etc/environment -- but a while ago this file was removed during some debugging, and not restored.
What does still surprise me is that
rails console staging
does not have the same effect as
export RAILS_ENV=staging
rails console
anyhow, sorted.

How to access database in heroku

So I would like to make an user to admin in the app I have on heroku. When developing my app I can do it via rails c or I just open up the database file with some SQL viewer. But how can I do it for heroku?
To run the rails console in heroku, you have to use heroku run rails console (assuming you have the heroku toolbelt installed).
See https://devcenter.heroku.com/articles/getting-started-with-rails3#console or https://devcenter.heroku.com/articles/getting-started-with-rails4#console

Rails - Undefined Method in Staging, but fine in local development

I'm working on a project and I added a migration to add a project_page_description field to a Company Model. When I've ran the migrations,
#company = Company.first
#company.project_page_description
works locally, but any time I attempt to access project_page_description after deploying, I get an undefined method error. Why does it work locally but not remotely?
I've deployed all code so the codebases are identical, I've ran all migrations, I'm using Bundler and Capistrano, and I'm deploying to a CentOS server.
Also, besides stop programming, what can I do to stop this from happening again?
Looks like the migrations have not been run. Maybe you ran them using the development environment? Try running them like so: RAILS_ENV=staging bundle exec rake db:migrate.
Check your bundler groups, application config, and environment initializer files, maybe you have something defined as being development-only and not staging? Also try opening a console up on staging and trying to manually load and invoke the module that's not found. Remember that require returns true if the module was not already loaded.

running Rails console in production

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

Resources