Find the bug, Rails edition
Blog
kitt decided around 13:45 on 20 February 2024 to publish this:
I have this code in a model in a Rails project.
validates :name, presence: true, on: :create validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }, on: create
Let's put it in an Author model:
class Author < ApplicationRecord validates :name, presence: true, on: create validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }, on: create end
When retrieving a list of all of the authors without email addresses (because, well, we are cleaning up these things) with this line of code:
authors = Author.where(:email: nil)
... two new blank authors are created at the WHERE query. Both of their names and emails are blank. This is absurd!
Find the bug.
I narrowed the problem down to those validates lines, but could not see the bug. After an hour of puzzling, I asked Katherine to look. When she said, "what the?" I felt a little bit better that this was something weird.
She figured it out, though.
class Author < ApplicationRecord validates :name, presence: true, on: :create validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }, on: :create end
F'ing symbols. Yay for Katherine's second set of eyes! Whoo!
Add new comment