0
0
Ruby on Railsframework~10 mins

Uniqueness validation in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Uniqueness validation
Start: New record input
Check if value exists in DB?
Add error
Reject save
End
Rails checks if the value already exists in the database before saving. If it does, it adds an error and rejects saving. Otherwise, it saves the record.
Execution Sample
Ruby on Rails
class User < ApplicationRecord
  validates :email, uniqueness: true
end
This code ensures that each User's email is unique before saving to the database.
Execution Table
StepInput EmailDB Check ResultValidation ResultAction
1"user@example.com"Not foundValidRecord saved
2"user@example.com"FoundInvalidSave rejected with error
3"new@example.com"Not foundValidRecord saved
💡 Execution stops when record is saved or rejected based on uniqueness check.
Variable Tracker
VariableStartAfter 1After 2After 3
emailnil"user@example.com""user@example.com""new@example.com"
validation_resultnilValidInvalidValid
record_savedfalsetruefalsetrue
Key Moments - 2 Insights
Why does the second record with the same email fail to save?
Because the uniqueness validation checks the database and finds the email already exists (see execution_table step 2), so it marks the record invalid and rejects saving.
Does uniqueness validation check only the current input or the whole database?
It checks the whole database for existing records with the same value before saving, as shown in the DB Check Result column in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the validation result at step 2?
AInvalid
BNot checked
CValid
DError
💡 Hint
Check the 'Validation Result' column at step 2 in the execution_table.
At which step does the record get saved with a new email?
AStep 1
BStep 2
CStep 3
DNo record is saved
💡 Hint
Look at the 'Action' column in the execution_table for when 'Record saved' happens with a new email.
If the email at step 3 was changed to "user@example.com", what would happen?
ARecord saved
BSave rejected with error
CValidation skipped
DDatabase error
💡 Hint
Refer to step 2 where the same email caused save rejection due to uniqueness validation.
Concept Snapshot
Uniqueness validation in Rails:
- Use validates :field, uniqueness: true
- Checks DB for existing value before save
- If found, adds error and rejects save
- Ensures no duplicate values in that field
- Works automatically on save/update
Full Transcript
Uniqueness validation in Rails ensures that a field's value is unique in the database before saving a record. When you try to save a record, Rails checks if the value already exists. If it does, it adds an error and prevents saving. If not, it saves the record. For example, validating uniqueness of email means no two users can have the same email. The execution table shows three steps: first saving a new email succeeds, second saving the same email fails, and third saving a different email succeeds. Variables like email, validation result, and record saved change accordingly. This helps prevent duplicate data in your app.