Understanding Rails · Season 2: Getting started
The console as a lab
Episode 15 · overview · about 2 minutes · Rails 8.1 · by Tommy A. Caruso
How was this episode?
The series' author reads every note, and no one else sees it. When something is wrong or unclear, it gets fixed and the episode re-cut. How this series stays right.
Not endorsed yet. Are you a Rails developer? What's an endorsement?
Endorse this version To endorse: create an account, confirm your email, then link GitHub.
What changed
- Version 3, November 21, 2026: A replay on a real app fixed the console output: a save in the sandbox uses a savepoint and never commits.
- Version 2, November 20, 2026: The sandbox rolls back only the database, not emails or calls to other services.
Try models, queries and helpers in bin/rails console, safely, with a sandbox.
Free to watch. If it helped, support the series.
Transcript
0:00 The console as a lab
Episode fifteen. The console as a lab. The console is your app, live, in Ruby. It's the fastest place to try an idea, and with one option, every change to the database rolls back.
0:16 --sandbox
Start it with bin slash rails console, dash dash sandbox. Rails tells you what that means: any changes you make will be rolled back when you exit.
0:27 Asking questions
Now ask questions. Note dot count: one. Note dot first gives you the note itself, and note dot body, its text: buy milk.
0:37 The SQL underneath
Under each answer, the console prints the SQL it ran. Count became select count. First became select, ordered by id, limit one. Watching this is how you learn what Active Record does.
0:50 Tracking changes
Change the body, and the note keeps track. Changed asks whether anything is different. Changes shows the old value beside the new one.
1:00 Saving
Save writes it to the database, with an update. Updated at moves on its own.
1:06 to_sql
To see a query's SQL without running it, end the query with to underscore SQL.
1:12 The lab finds a bug
Now something worth noticing. Create, with an empty body, works. Note has no validations, so it takes a note with nothing in it. Your lab just found a bug to fix.
1:24 View helpers
Helper reaches the view helpers. Pluralize, three, note, gives three notes. Number to currency, four point five, gives four dollars fifty.
1:34 reload!
Edited a file while the console was open? Reload bang loads your latest code without leaving.
1:41 Rolled back
Now exit. Because of the sandbox, the oat milk and the empty note are gone. The database is just as it was: one note, buy milk.
1:51 On a server
That matters most on a server. A console in production touches real data, so open it with dash dash sandbox until you mean to change something. The sandbox rolls back the database, not emails or calls to other services, so keep those sessions short.
Next: environments, configuration, and credentials.
Check your understanding
-
In bin/rails console --sandbox, you create two notes and then exit. You open a normal console. What does Note.count show?
- The old count, because the sandbox rolled back your changes
- The old count plus two
- Zero, because the sandbox empties the database
- An error, because the sandbox locked the table
Show answer
The old count, because the sandbox rolled back your changes. Sandbox mode wraps everything in a transaction that rolls back when you exit. Source: bin/rails console, The Rails Command Line (Rails Guides 8.1).
-
A note's body is "Buy milk". You set note.body = "Buy oat milk" and don't save. What does note.changes return?
- {}
- {"body" => "Buy oat milk"}
- true
- {"body" => ["Buy milk", "Buy oat milk"]}
Show answer
{"body" => ["Buy milk", "Buy oat milk"]}. changes gives each changed attribute with its original value and its new value. Source: ActiveModel::Dirty#changes (Rails API 8.1).
-
You want to see the SQL for Note.where("body LIKE ?", "%milk%") without running it. What do you add?
- .explain
- .to_sql
- .load
- .inspect
Show answer
.to_sql. to_sql returns the SQL statement for the query as a string. Source: ActiveRecord::Relation#to_sql (Rails API 8.1).
-
In the console, what does helper.pluralize(3, "child") return?
- "3 childs"
- "3 child"
- "3 children"
Show answer
"3 children". helper reaches the view helpers, and pluralize knows irregular words like child. Source: The helper Object, The Rails Command Line (Rails Guides 8.1).
Sources
Every example in this episode was run on Rails 8.1.4.1 and Ruby 4.0.2.