DEV Community

loading...

Rails quick tips #1: Console sandbox

citizen428 profile image Michael Kohl Originally published at citizen428.net Updated on ・1 min read

This is the first in a series of Ruby on Rails related quick tips I'm going to publish over the next few days/weeks.

If I had to name the most underused tool in most Rails developer's toolboxes, rails console --sandbox would be my choice. Here's what the documentation has to say on it:

If you wish to test out some code without changing any data, you can do that by invoking rails console --sandbox.

Here's an example sandbox console session:

→ rails c --sandbox
Loading development environment in sandbox (Rails 5.2.0)
Any modifications you make will be rolled back on exit
[1] (rails_new) main: 0> User.count
   (17.7ms)  SELECT COUNT(*) FROM "users"
=> 1
[2] (rails_new) main: 0> User.destroy_all
  User Load (0.4ms)  SELECT "users".* FROM "users"
   (1.5ms)  SAVEPOINT active_record_1
  User Destroy (7.4ms)  DELETE FROM "users" WHERE "users"."id" = $1  [["id", 1]]
   (0.7ms)  RELEASE SAVEPOINT active_record_1
=> [#]
[3] (rails_new) main: 0> User.count
   (0.3ms)  SELECT COUNT(*) FROM "users"
=> 0
[4] (rails_new) main: 0>
   (0.8ms)  ROLLBACK

As can be seen above the last SQL command executed in this console session was ROLLBACK, so we're leaving everything just the way we originally found it.

That's it for today, I'll be back with another database related Rails quick tip soon.

Discussion (10)

pic
Editor guide
Collapse
dwilkins profile image
David H. Wilkins

Yes, --sandbox is very handy, but I cringe every time I see someone use it on a production system.

Sandbox mode doesn't just rollback, it creates an enclosing transaction for all of the commands you do while in that instance of the console. If you change database rows in that transaction, updates to the tables and rows affected can block for other users. This can cause lockups for other users if used in production.

Any rails console actions on production should be done carefully, and should (IMHO) never use --sandbox. Sandbox mode is really useful for development and other non-critical systems.

Collapse
captainawesomedi profile image
Di Wu

I wish I read your comments earlier today. I was testing out something in sandbox in product apparently triggered something recursive locked all records and Database cpu usage was 100%. Our application was down for 30-45 minutes. It recovered till someone restart the database instance 😭

Collapse
citizen428 profile image
Michael Kohl Author

Good point! To be honest I never even thought about someone using this on a production app :O

Collapse
ben profile image
Ben Halpern

Heck yes! Subscribed to this series big time. @andy @jess @maestromac follow Michael if you aren't.

Collapse
andy profile image
Andy Zhao (he/him)

Very nice! That's going to be super useful.

Looking forward to more Rails tips!

Collapse
andy profile image
Andy Zhao (he/him)

lol @citzen428 I'm rediscovering this six months later and realizing how useful it's going to be... again!

Collapse
philnash profile image
Phil Nash

I don't know how I've missed this, thanks for the tip!

Collapse
maestromac profile image
Mac Siri

If only I knew this sooner!

Collapse
didin1453fatih profile image
Comment marked as low quality/non-constructive by the community. View Code of Conduct
Didin 👨‍💻

I think you can combine Rails and dbdesigner.id as your database designer. This can make your project readable and clear documentation.