Understanding Rails · Season 1: The trip of one click
Models
Episode 5 · 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 2, November 20, 2026: A check against the sources changed "SQLite is ready for production" to "SQLite is enough in production for many sites".
Active Record, migrations, validations, the SQL it writes, SQLite.
Free to watch. If it helped, support the series.
Transcript
0:00 Models
Part five. Models.
A model is where your app's rules live. Each model is a Ruby class, backed by a table in the database. Rails calls this Active Record.
0:16 Migrations
Tables come from migrations: small Ruby files that change the database one step at a time. This one creates the notes table.
Every note has a trail, an author, and a body, plus timestamps that Rails fills in for you.
0:33 The model
Here's the note model. Two belongs to lines say how it connects to trails and users. And a validation says a note must have a body.
0:43 Validations
When the controller calls save, the model checks its rules first. A blank note fails, and save returns false. That's the four twenty two from part four.
0:54 The SQL it writes
If the rules pass, Active Record writes the SQL for you. One insert, carrying our note's values.
Reading works the same way. Remember set trail? Trail dot find, forty two, became a select.
1:09 SQLite
Where does it all go? A new Rails 8 app keeps it in SQLite, a database that lives in a single file. For many sites, it's enough in production too.
The note is saved, with the ID three seventeen. Now Rails has to show it. That's the view, in part six.
Check your understanding
-
A note with an empty body fails its presence validation. What does note.save return?
- It raises an error
- nil
- false
- The note, unsaved
Show answer
false. save returns false when a validation fails, and nothing is written. save! is the one that raises. Source: Validation Triggers, Active Record Validations (Rails Guides 8.1).
-
A new note passes its validations and saves for the first time. Which SQL does Active Record send?
- An INSERT
- An UPDATE
- A SELECT, then nothing
- A CREATE TABLE
Show answer
An INSERT. Saving a new record sends an INSERT. Saving one that already exists sends an UPDATE. Source: Validation Triggers, Active Record Validations (Rails Guides 8.1).
-
What does Trail.find(42) turn into?
- A SELECT for every trail, filtered in Ruby
- An INSERT of trail 42
- A SELECT for the trail with id 42, limited to one
- Nothing until you call .first on it
Show answer
A SELECT for the trail with id 42, limited to one. find looks up one record by its primary key. The SQL has WHERE id = 42 and LIMIT 1. Source: find, Active Record Query Interface (Rails Guides 8.1).
-
The notes table has created_at and updated_at from t.timestamps. Who fills in created_at?
- The controller, from the form
- Rails, with the current time when the note is first saved
- The database, only if you write a trigger
- Nobody: it stays empty until you set it
Show answer
Rails, with the current time when the note is first saved. Active Record sets created_at to the current date and time when the record is first created. Source: Schema Conventions, Active Record Basics (Rails Guides 8.1).