📖 Scenario: You are building a simple Rails app to manage users. Each user must have a unique email address so that no two users can register with the same email.
🎯 Goal: Create a Rails model User with an email attribute. Add a uniqueness validation to ensure no duplicate emails can be saved.
📋 What You'll Learn
Create a User model with an email attribute
Add a uniqueness validation on the email attribute in the User model
Use Rails built-in validation syntax
Ensure the validation is case insensitive
💡 Why This Matters
🌍 Real World
Ensuring unique emails is essential for user registration systems to avoid account conflicts.
💼 Career
Understanding Rails validations and database constraints is key for backend developers building secure and reliable web apps.
Progress0 / 4 steps
1
Create the User model with email attribute
Create a Rails model called User with a string attribute email.
Ruby on Rails
Hint
In Rails, the model class inherits from ApplicationRecord. The email attribute comes from the database migration.
2
Add uniqueness validation configuration
Add a validation line in the User model to check uniqueness of the email attribute with case insensitivity.
Ruby on Rails
Hint
Use validates :email, uniqueness: { case_sensitive: false } to ensure emails are unique ignoring case.
3
Add presence validation for email
Add a validation line in the User model to ensure the email attribute is present (not empty).
Ruby on Rails
Hint
Use validates :email, presence: true to require an email value.
4
Add database index for uniqueness
Add a unique index on the email column in the database migration to enforce uniqueness at the database level.
Ruby on Rails
Hint
Use add_index :users, :email, unique: true in the migration to prevent duplicates in the database.
Practice
(1/5)
1. What does validates :email, uniqueness: true do in a Rails model?
easy
A. Deletes duplicate email records from the database
B. Prevents saving records with duplicate email values
C. Allows multiple records with the same email
D. Automatically encrypts the email field
Solution
Step 1: Understand uniqueness validation purpose
Uniqueness validation ensures no two records have the same value for the specified field.
Step 2: Apply to the email field
Using validates :email, uniqueness: true means Rails will check that the email is unique before saving.
Final Answer:
Prevents saving records with duplicate email values -> Option B
Quick Check:
Uniqueness validation = prevents duplicates [OK]
Hint: Uniqueness validation stops duplicate field values [OK]
Common Mistakes:
Thinking it encrypts data
Assuming it deletes duplicates automatically
Believing it allows duplicates
2. Which is the correct syntax to add uniqueness validation for a username in a Rails model?
easy
A. validates :username, unique: true
B. validate :username, uniqueness: true
C. validates :username, uniqueness: true
D. validates_uniqueness_of :username, true
Solution
Step 1: Recall Rails validation syntax
Rails uses validates :field, uniqueness: true for uniqueness validation.
Step 2: Check each option
validates :username, uniqueness: true matches the correct syntax exactly; others have syntax errors or deprecated style.