Challenge 2: A Custom Validation — Solution class Event < ApplicationRecord validate :end_time_after_start_time private def end_time_after_start_time return if start_time.blank? || end_time.blank? if end_time <= start_time errors.add(:end_time, "must be after the start time") end end end =begin Notes: - No single built-in validator (presence, length, numericality, etc.) can express "one field must come after another field" -- that requires comparing two attributes against each other, which is exactly what a custom validation method is for. - validate :end_time_after_start_time (singular "validate") registers a custom instance method, distinct from validates (plural), which is reserved for the built-in attribute validators shown in Challenge 1. - return if start_time.blank? || end_time.blank? guards against running the comparison at all when either field is missing -- letting the ordinary presence validators (not shown here, but would typically also be present) handle that case separately, rather than raising a confusing NoMethodError trying to compare nil to nil. - errors.add(:end_time, "...") attaches the error specifically to end_time, which matters later for form rendering (Chapter 8) — Rails can highlight exactly which field the error belongs to. =end