0
0
Ruby on Railsframework~30 mins

Uniqueness validation in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style9 modes available
Uniqueness validation
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use add_index :users, :email, unique: true in the migration to prevent duplicates in the database.