0
0
Ruby on Railsframework~10 mins

Custom validation methods in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom validation methods
Start: Model instance created
Call validate method
Run custom validation method
Check condition inside method
No error
Validation result
Save or reject model
When saving a model, Rails calls custom validation methods that check conditions and add errors if needed, deciding if the model is valid.
Execution Sample
Ruby on Rails
class User < ApplicationRecord
  validate :email_must_be_company

  def email_must_be_company
    unless email&.end_with?('@company.com')
      errors.add(:email, 'must be a company email')
    end
  end
end
This code checks if a user's email ends with '@company.com' and adds an error if it doesn't.
Execution Table
StepActionCondition CheckedResultErrors Added
1Create User instance with email 'user@gmail.com'N/AInstance createdNone
2Call validate method on UserN/ACustom validation method calledNone
3Check if email ends with '@company.com'email = 'user@gmail.com'FalseAdd error: 'email must be a company email'
4Validation resultErrors present?YesUser invalid, save rejected
5Create User instance with email 'employee@company.com'N/AInstance createdNone
6Call validate method on UserN/ACustom validation method calledNone
7Check if email ends with '@company.com'email = 'employee@company.com'TrueNo error added
8Validation resultErrors present?NoUser valid, save allowed
💡 Validation stops after checking condition; errors determine if save proceeds.
Variable Tracker
VariableStartAfter Step 3After Step 7Final
emailN/A'user@gmail.com''employee@company.com'Depends on instance
errorsEmptyContains error for emailEmptyDepends on instance
validation_resultN/AInvalidValidDepends on instance
Key Moments - 2 Insights
Why does the validation fail for 'user@gmail.com' but pass for 'employee@company.com'?
Because the custom method checks if the email ends with '@company.com'. At step 3, 'user@gmail.com' fails this check and adds an error, while at step 7, 'employee@company.com' passes and adds no error.
What happens if the email is nil or empty?
The method uses safe navigation (&.) to avoid errors. If email is nil, the condition returns false, so an error is added, preventing save.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what error is added at step 3?
A'email cannot be blank'
B'email is invalid format'
C'email must be a company email'
DNo error added
💡 Hint
Check the 'Errors Added' column at step 3 in the execution table.
At which step does the validation method confirm the email is valid?
AStep 7
BStep 3
CStep 4
DStep 8
💡 Hint
Look for the step where the condition is true and no errors are added.
If the email was 'admin@company.org', what would happen at step 3?
ANo error added, validation passes
BError added, validation fails
CMethod raises an exception
DValidation skipped
💡 Hint
The method checks if email ends with '@company.com'; 'admin@company.org' does not.
Concept Snapshot
Custom validation methods in Rails let you add your own checks inside models.
Define a method that adds errors if conditions fail.
Use 'validate :method_name' to run it during validation.
If errors are added, the model is invalid and won't save.
Use safe checks to avoid errors with nil values.
Full Transcript
In Rails, custom validation methods let you check specific rules for your model data. When you save a model, Rails calls these methods. Inside the method, you check a condition, like if an email ends with '@company.com'. If it doesn't, you add an error message. This error stops the model from saving. For example, if a user has 'user@gmail.com', the validation adds an error and save fails. But if the email is 'employee@company.com', no error is added and save succeeds. This process helps keep your data clean and correct.