Revert rails console changes - ruby-on-rails

I have an application where I run rake tasks to add data from csv files. I need to test it from the console first. Is there a command in console to revert all the changes as its meant only for testing.

You need to run the console in sandbox mode.
rails c --sandbox

you can run:
bundle exec rails c -s
with -s option all commands are executed in sandbox mode. After exiting the console all commands will be rolled back. (you can use --sandbox or the shorter form -s)

you can start the console in sandbox mode. It will wrap the whole session in a db transaction which will be rollbacked when you close the console.
To start it:
rails console --sandbox

Related

how to access stdout console in rails app running on Ubuntu VPS

I need to run this Rails.logger.info "SMTP_ADDR: #{ENV["SMTP_ADDRESS"].inspect}" in stdout console on my VPS
I'm not sure how to access the stdout console
can someone please advise?
I'm not sure what you mean by stdout, but if you need run this on your VPS environment, rails console probably will help you.
For production environment, run it in the app directory:
RAILS_ENV=production bundle exec rails console
Omit or change rails_env variable for another environment.

Rails server run under production mode but access database under development-database

I found an strange issue.
I run rails server -e production, and put binding.pry in my controller.
And the Resque rake resque:work QUEUE='*' will trgger the method later.
First, it will create a new instance in production database.
Then, I dig into binding.pry
I try to pull all the data from the model by MODEL.all
Unexpectedly, I found the data is all from development database.
So, I ran into console rails c and RAILS_ENV=production rails c to check what's the problem
I found the instance was created at production mode correctly, but how come in the following methods will pull the data from development mode??
There are Passenger and Nginx run on my server and I run another server by rails server -e production to debug.
Thanks

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

Rails 3 Sandbox Console

In Rails 2 you're able to run
script/console --sandbox
so you can play with production data and not accidentally break anything.
I can't seem to find the equivalent command for Rails 3. Does anyone know what it is?
Easy, type in:
bundle exec rails c -s
and that is it.
$ bundle exec rails c --help
Usage: console [environment] [options]
-s, --sandbox Rollback database modifications on exit.
--debugger Enable ruby-debugging for the console.
--irb DEPRECATED: Invoke `/your/choice/of/ruby script/rails console` instead
It is simple, but, sometimes, if you are not running rails executable using bundle exec, it may, or may not, result in an error. In order to avoid this, ALWAYS use bundle exec.
To quote bundler page (if not documentation):
In some cases, running executables without bundle exec may work, if
the executable happens to be installed in your system and does not
pull in any gems that conflict with your bundle.
However, this is unreliable and is the source of considerable pain.
Even if it looks like it works, it may not work in the future or on
another machine.

Rails: rake db:migrate isn't output to script/server console window

i have two cmd consoles opened in Windows. One is running the "ruby script/server" the other i run a "rake db:migrate" command. But the server console doesn't output the log of the migration? Someone has an idea what's the reason?
You just see this log in your log file. The server log only what happen in this thread, no in other.

Resources