Skip to content

Instantly share code, notes, and snippets.

@giljr
Last active November 22, 2025 20:34
Show Gist options
  • Select an option

  • Save giljr/13a433d4e6f1efa75deed0baa112ff7d to your computer and use it in GitHub Desktop.

Select an option

Save giljr/13a433d4e6f1efa75deed0baa112ff7d to your computer and use it in GitHub Desktop.
Rails Model Validation Best Practices (Full Guide) Your complete, professional, Rails-grade guide to Model Validation Best Practices

The Default Rails Validators:

             validates :attribute, validator_name: options

You can memorize the core validators in 10 seconds:

Validator Purpose Example
presence: must not be blank presence: true
absence: must be blank absence: true
confirmation: requires a matching field confirmation: true
acceptance: typical checkbox terms: acceptance: true
numericality: must be number numericality: true
length: length constraints length: { max: 100 }
inclusion: value must be in list/range inclusion: 1..5
exclusion: must not be in list exclusion: ["admin"]
format: regex match format: /@/

Rails maps the

validator_name to a validator class:

      presence: → PresenceValidator

       length: → LengthValidator

 numericality: → NumericalityValidator

       format: → FormatValidator

   custom ones → EmailValidator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment