Skip to content

Commit ecfb9f4

Browse files
committed
Add Prevent Writes With A Sandboxed Rails Console as a Rails til
1 parent c448814 commit ecfb9f4

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For 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)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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)

0 commit comments

Comments
 (0)