Understanding Rails · On the job · Ticket 001
Notes vanish sometimes
Episode 101 · deep dive · about 12 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.
A senior works ticket 001 out loud: refine it, reproduce it from the log, a failing test, the fix and its trap, and CI's review of the pull request.
Free to watch. If it helped, support the series.
Transcript
0:00 Notes vanish sometimes
On the job. Ticket one: notes vanish sometimes. So far, we've built things from scratch. Most of a Rails job is different. You work in an app someone else wrote, on tickets someone else wrote. Today I work one ticket in Switchback, out loud, from the first read to the review. And the work doesn't start with code. It starts at the ticket.
0:25 The ticket
Here it is, the way tickets really arrive. The product manager forwards a message from support: a few people have said something like it. Dana, a regular hiker, writes: I posted a long note about the washout on Granite Switchbacks last night. It said note posted, but when I looked for it this morning, it wasn't there. This is the second time. Are you deleting notes?
0:48 Shovel ready
I could open the code now and start guessing. I won't, because this ticket isn't ready. A ticket is ready, people say shovel ready, when someone can start it without a question that blocks them. We know who hits the problem, where, what they expected, and what happened instead. We can reproduce it. Done is written down, as criteria a test can check. And what's out of scope is said out loud.
So start with what the ticket already says. Read it slowly. Three clues matter. Long: it was a long note. Note posted: the app said it worked. And it wasn't there: when Dana looked the next morning, the note was gone. Dana thinks we deleted it. That's a guess about the cause, and reporters often guess. Keep the facts, and hold the guess loosely.
1:34 Questions for the reporter
Now, questions for Dana. A good question splits the possible causes in two. Did you see an error? No. It said note posted. Did anyone else ever see the note? No one ever saw it. That answer matters most. A deleted note would have been there for a while first. A note nobody ever saw may never have been saved.
Were the lost notes long? Yes, I write a lot, especially after a washout. Do your short notes stay? Yes, those are all still there. Which trail, and which browser? Granite Switchbacks, but Eagle Ridge too, in Chrome on a laptop. Two trails, so it isn't one trail's data. The browser, we'll find out when we reproduce it.
2:15 A hypothesis
So, a hypothesis. Only a hypothesis, until we prove it. Long notes are lost, short ones stay, and the app says note posted either way. Something about length. The note model has a rule: the body is required, and at most five hundred characters. And does anything delete notes? Search the app for destroy and delete. A note goes only when its trail or its author is deleted. Nothing deletes one note.
2:41 Reproduce it
Now reproduce it, the way Dana did. Sign in, open Granite Switchbacks, and post a long note: five hundred and seventy nine characters about a washout. The page says note posted. Under trail notes, there's nothing. Post a short note, and it shows up. We can make it happen every time. That's the most useful thing a bug report can get.
3:03 The log
The browser shows what Dana saw. The log shows what the app did. Open the development log, and find the post. The parameters hold the whole note. Then it loads the session, the trail, and the user. Then: redirected, three oh two found. There's no insert. Nothing ever tried to write the note to the database.
Compare the short note. The same lines, then begin transaction, an insert into notes, and commit. So the long note wasn't deleted. It was never saved. Dana asked if we're deleting notes. The truth is that we never kept them.
One more check, with a short script run by bin rails runner. Build that same note, and call save. It returns false. The errors say: body is too long, maximum is five hundred characters. The model did its job. It refused the note, and said why. Nobody listened.
3:58 The cause
Who didn't listen? Open the notes controller. Create builds the note, sets its author, and calls save. Then, whatever save returned, it redirects with note posted. Save returned false, and nothing checked. That's the cause.
4:13 Acceptance criteria and scope
Now we can write the ticket properly. A title that says what's wrong: a note over five hundred characters is lost without an error. Then acceptance criteria, each one something a test can check. One: a note over five hundred characters isn't saved, and the hiker is told why. Two: the hiker's words stay in the form, so nothing they wrote is lost. Three: the page answers four twenty two, so Turbo shows the form again. Four: note posted shows only when a note was saved. Five: a note of five hundred characters still posts as before.
Then what's out of scope, said out loud, so nobody builds it by accident. We won't change the five hundred character limit. We won't show the limit, or a counter, before the hiker hits it. That's a good next ticket. And we can't get back notes already lost. They were never saved, so there's nothing to restore.
Check it against shovel ready. We know who, where, what they expected, and what happened. It reproduces every time. The criteria can be tested, and the scope is said. It fits in one pull request. No question is left that blocks starting. Now, and only now, code.
5:25 A failing test
First, a test that fails because of the bug. In the notes controller test: sign in as Sam, and post a note of five hundred and one characters, one over the limit. Assert the count of notes doesn't change. Assert the response is unprocessable content, the four twenty two. And assert the text area still holds the words, and the page says too long.
Run it. One failure, and it's the right one. Expected response to be a four twenty two, but was a three oh two found, a redirect. That's the bug, in the test's own words. Now we know the test can see it.
6:02 The fix
Now the fix. If save works, redirect with note posted. If it doesn't, render the trail page again, with status unprocessable content. And on the trail page, above the form, show the note's errors when it has any. It looks right.
6:18 The trap
Run the test. It isn't red, and it isn't green. It's an error. Action view template error: nil can't be converted to a Time value. In the note partial, line two, where each note says how long ago it was posted. Something on the page is a note with no created at time.
Which note has no time? The one that just failed to save. The controller built it with trail dot notes dot new. Building through the association does more than set the trail. It adds the new note to the trail's list of notes, saved or not. The trail has two notes. After trail dot notes dot new, its list has three, and the third has no id and no time. A failed save leaves it there.
Before, nobody saw this, because create always redirected, and the next request started fresh. Now we render the page in the same request, and the trail's notes still hold the unsaved one. The page tries to draw it, and fails.
7:16 The fix, again
So build the note on its own: note dot new, with the params, the trail, and the author. It still belongs to the trail, but the trail's list doesn't hold it. Run the test file: three runs, no failures. Post the long note again: four twenty two, the error above the form, and every word still in the text area.
7:36 bin/ci and the commit
Before pushing, run what C I runs, on my own machine. Bin C I: the style check, the security audits, Brakeman, the tests, and the seeds. All of them pass.
Then a commit message that says why, not only what. A note over five hundred characters failed validation, but create ignored the failed save and said note posted, so the hiker's note was lost without a word. And a second paragraph about the trap, so the next person doesn't walk into it: build the note with note dot new, not through the trail.
8:10 The pull request
Push the branch, and open a pull request. The branch name starts with the ticket number: zero zero one, notes vanish. In Switchback, C I is the first reviewer. For a branch named for a ticket, it runs that ticket's acceptance tests, and comments on what passed.
8:28 CI's review
Here's its comment. A note over five hundred characters isn't saved: pass. The words stay in the form: pass. Four twenty two: pass. Note posted only when saved: pass. Five hundred still posts: pass. Look at those lines. They're our acceptance criteria, word for word. Refining the ticket is what gave C I something to check.
8:51 Answering the reviewer
Passing isn't the same as approved. So the comment adds what a human reviewer would still ask. Answer each one, the way you would on a real pull request.
Where is your test? In the notes controller test. It failed before the fix, for the right reason. If the bug comes back, it fails again.
Did you hit the unsaved note in the list? I did. That was the trap. The note is built with note dot new now, and if someone builds it through the trail again, the same test errors.
Would a hiker understand the error? Read it out loud, the way Dana would see it. Body is too long. Body is a name from our code. Dana never sees that word. The page says how was the trail, and post note. Dana's word is note.
Rails builds the message from the attribute's human name, then the message. So give the attribute a hiker's name, in the locale file. Active record, attributes, note, body: your note. Now it says, your note is too long, maximum is five hundred characters. A blank note says, your note can't be blank. And the acceptance tests still pass.
Does your commit message say why? The fix's message says what went wrong for the hiker, and why the note is built the way it is. The wording change says why too: body names our code, and your note names what Dana wrote.
Anything you noticed but left alone? Yes. A hiker can't see the limit until they hit it. That's out of scope, and it's a good next ticket. Write it down, and leave it. A pull request that does one thing is easier to review.
10:31 Closing the loop
Last, close the loop with Dana. We weren't deleting notes. Long notes were never saved, and the app said they were. Now it says why, and keeps every word. The notes already lost can't come back, and the reply should say that plainly.
10:47 The rules
So: the work starts at the ticket. Read it for facts, not for the reporter's guess. Ask questions that split the causes. Reproduce it, in the browser and in the log. Write criteria a test can check, and say what's out of scope. Then a failing test, the fix, and a review you can answer.
11:05 Your turn
Now it's your turn. Ticket one has its own page, where you ask Dana your own questions before you see ours. Fork Switchback, make a branch named for the ticket, and open a pull request in your fork. C I reviews it. Push only a failing test, and it says one of five pass. Each cross is something the hiker still runs into.
Check your understanding
-
A controller calls @note.save, then always redirects with "Note posted." What happens to a note that fails validation?
- Rails raises, so the hiker sees an error page
- save returns false, nothing is written, and the hiker is still told it was posted
- Rails saves it anyway, without the invalid part
Show answer
save returns false, nothing is written, and the hiker is still told it was posted. save runs the validations and returns false when the record is invalid, without writing it. Code that doesn't check that return value carries on as if it worked. Source: Active Record Validations: Validation Triggers (Rails Guides 8.1).
-
The controller builds a note with @trail.notes.new, the save fails, and it renders the trail page. Why does the page try to draw the unsaved note?
- Because building through the association adds the new note to the trail's loaded notes, and a failed save doesn't remove it
- Because a failed save writes a draft row
- Because render reloads every note from the database, including invalid ones
Show answer
Because building through the association adds the new note to the trail's loaded notes, and a failed save doesn't remove it. CollectionAssociation#build, which new calls, adds the record it builds to the association's target. It stays there whether or not it's saved. Note.new(trail:) sets the trail without touching the trail's list. Source: CollectionAssociation#build (Rails source, v8.1.4.1).
-
A form submitted through Turbo fails validation. What should the server answer, so Turbo shows the form again with its errors?
- A 302 redirect back to the form
- A 200 with the form
- A 422 Unprocessable Content with the form
Show answer
A 422 Unprocessable Content with the form. Turbo Drive expects a redirect after a form submission, except when the response is a 4xx or 5xx. A 422 lets the form render with its errors. Source: Turbo Handbook: Redirecting After a Form Submission.
-
An error reads "Body is too long (maximum is 500 characters)". How do you make it say "Your note is too long" everywhere, without changing the validation?
- Rename the body column to your_note
- Set activerecord.attributes.note.body to "Your note" in a locale file
- Pass message: "Your note is too long" to every view
Show answer
Set activerecord.attributes.note.body to "Your note" in a locale file. full_messages puts the attribute's human name before the message, and human_attribute_name looks that name up under activerecord.attributes in the locale files. Source: Rails Internationalization: Active Record Models (Rails Guides 8.1).
Sources
Every example in this episode was run on Rails 8.1.4.1 and Ruby 4.0.10.
- Active Record Validations: Validation Triggers (Rails Guides 8.1)
- Active Record Validations: length (Rails Guides 8.1)
- Debugging Rails Applications: The Logger (Rails Guides 8.1)
- Testing Rails Applications: Rails-Specific Assertions (Rails Guides 8.1)
- Turbo Handbook: Redirecting After a Form Submission
- CollectionAssociation#build (Rails source, v8.1.4.1)
- CollectionProxy#new, an alias of build (Rails source, v8.1.4.1)
- Rails Internationalization: Active Record Models (Rails Guides 8.1)
- Rails Internationalization: Active Model Methods (Rails Guides 8.1)