MΓ³dulo 07 Β· ActiveRecord β LecciΓ³n 2 de 5 Β· ~11 min
Validations
A migration enforces the database. A validation enforces Ruby.
null: false on the dogs.name column stops a nameless row from ever reaching the database β but it does that with a low-level SQL error, which is a harsh way to find out. Validations live in the model instead, run in Ruby before any SQL fires, and give you back a friendly, structured list of what's wrong:
class Dog < ApplicationRecord
validates :name, presence: true
validates :size, inclusion: { in: %w[small medium large] }
endThis is the SAME validation the real PawWalk backend's Dog model has β open apps/pawwalk-api/app/models/dog.rb in a later module and you'll recognize both lines exactly.