Ever inherited a project with a database full of messy, inconsistent data? Maybe you’ve dealt with users submitting forms filled with errors or blank fields? That’s where active record validations come to the rescue, and trust me—they’re a must-have in every developer’s toolkit.
Why Validations Matter
Validations are more than just error prevention—they’re your first line of defense for maintaining data integrity. Clean, validated data means fewer bugs, smoother user experiences, and more peace of mind for everyone involved.
Active record validations are your best friend for ensuring your app’s data is:
- Present
- Unique
- Properly formatted
- Just the right size
- Always reliable
Here’s a quick guide to active record validations that’ll keep your data clean, consistent, and reliable.
- Presence Validation
Don’t let your users leave fields blank—especially the important ones! Use presence validation to ensure that critical attributes, like name
or email
, are always filled.
1
validates :name, presence: true
Without this, you might end up with nameless users (or worse, errors down the road).
- Uniqueness Validation
Duplicates? Not on our watch! Ensure that certain fields, like emails or usernames, stay unique across your database.
1
validates :email, uniqueness: true
Pro tip: Combine this with a database uniqueness constraint for extra protection!
- Length Validation
Sometimes, size does matter. Avoid usernames that are too short to identify or too long to fit on the screen with length validations.
1
validates :username, length: { in: 3..20 }
Strike the perfect balance between too short and too long—just like Goldilocks.
- Format Validation
Emails that don’t look like emails? Passwords missing special characters? Use format validations with regex to keep your data in check.
1
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }
Because example@not-an-email
just doesn’t cut it.
- Numericality Validation
Numbers should be numbers—no words allowed. Use this validation to ensure attributes like price
, age
, or quantity
are numerical values.
1
validates :price, numericality: { greater_than: 0 }
No more “free” products with negative prices!
- Inclusion & Exclusion Validation
Sometimes, you want to define a club (or blacklist). Use inclusion to allow only certain values and exclusion to block unwanted ones.
1
2
validates :role, inclusion: { in: %w[admin user guest] }
validates :username, exclusion: { in: %w[admin superuser] }
Keep things in or out—your app, your rules.
- Acceptance Validation
Want to make sure users agree to your terms of service? Acceptance validation ensures that checkbox gets checked.
1
validates :terms_of_service, acceptance: true
We all totally read them before accepting, right?
- Confirmation Validation
Passwords not matching? Not anymore! Confirmation validation ensures fields like passwords and email addresses are entered twice for verification.
1
validates :password, confirmation: true
Because typos happen—even to the best of us.
- Custom Validations
Sometimes, the built-in validations just aren’t enough. Write custom validations for those unique business rules or complex logic.
1
2
3
4
5
validate :check_age
def check_age
errors.add(:age, "must be at least 18") if age.present? && age < 18
end