File tree Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010
1111For a steady stream of TILs, [ sign up for my newsletter] ( https://tinyletter.com/jbranchaud ) .
1212
13- _ 1150 TILs and counting..._
13+ _ 1151 TILs and counting..._
1414
1515---
1616
@@ -723,6 +723,7 @@ _1150 TILs and counting..._
723723- [ Perform SQL Explain With ActiveRecord] ( rails/perform-sql-explain-with-activerecord.md )
724724- [ Polymorphic Path Helpers] ( rails/polymorphic-path-helpers.md )
725725- [ Pretend Generations] ( rails/pretend-generations.md )
726+ - [ Prevent Writes With A Sandboxed Rails Console] ( rails/prevent-writes-with-a-sandboxed-rails-console.md )
726727- [ Query A Single Value From The Database] ( rails/query-a-single-value-from-the-database.md )
727728- [ Read In Environment-Specific Config Values] ( rails/read-in-environment-specific-config-values.md )
728729- [ Read-Only Models] ( rails/read-only-models.md )
Original file line number Diff line number Diff line change 1+ # Prevent Writes With A Sandboxed Rails Console
2+
3+ I often open a ` rails console ` to play around with some data and make sure I
4+ understand how some models can be instantiated while respecting their
5+ associations. There are plenty of times where I've created some data in the
6+ ` development ` database that doesn't need to be there. It may even be incomplete
7+ data from a failed experiment.
8+
9+ This data accumlates and clutters up the database.
10+
11+ One way to avoid this is by running the console in a sandboxed mode. Include
12+ the ` --sandbox ` flag when starting up a session to do this.
13+
14+ ``` bash
15+ $ rails console --sandbox
16+ Loading development environment in sandbox (Rails 5.2.6)
17+ Any modifications you make will be rolled back on exit
18+ [1] pry(main)>
19+ ```
20+
21+ This wraps the session in a transaction so that any writes to the database can
22+ be rolledback afterward.
23+
24+ [ source] ( https://dev.to/citizen428/rails-quick-tips-1-console-sandbox-4k0c )
You can’t perform that action at this time.
0 commit comments