rails c or
rails console starts the irb session, where we can execute, test and confirm queries. It is one of the helpful feature for developers. However we need to use it with caution as it is capable of modifying records. Any accidental modification in staging or production data might lead to problem.
There is an option to avoid this. Rails comes with an inbuilt
sandbox mode. When you start the console with
--sanbox flag, we will see a message
Any modifications you make will be rolled back on exit because the whole irb session starts within an ActiveRecord::Transaction, which is rolled back when you exit the session.
Here's a sample
But wait, There is a catch
In sandbox mode, database rows are fully locked, which means that it prevents your live production services from modifying them. If you are holding a record for too long, all the live transactions related to it have to wait till the lock is released or in some cases might mess up the updations.