Recall & Review
beginner
What is the purpose of uniqueness validation in Rails?
Uniqueness validation ensures that a specific attribute value is unique in the database, preventing duplicate records for that attribute.
Click to reveal answer
beginner
How do you add a uniqueness validation to a model attribute in Rails?
Use
validates :attribute_name, uniqueness: true inside the model to enforce uniqueness on that attribute.Click to reveal answer
intermediate
Why should you add a unique index in the database when using uniqueness validation?
Because Rails validations run in application code and can miss race conditions, a unique database index ensures data integrity at the database level.
Click to reveal answer
intermediate
What option do you use to make uniqueness validation case insensitive?
Use
validates :attribute_name, uniqueness: { case_sensitive: false } to ignore case differences when checking uniqueness.Click to reveal answer
intermediate
How can you validate uniqueness scoped to another attribute in Rails?
Use
validates :attribute_name, uniqueness: { scope: :other_attribute } to ensure uniqueness within the scope of another attribute.Click to reveal answer
Which Rails method adds uniqueness validation to a model attribute?
✗ Incorrect
The correct syntax is
validates :attribute, uniqueness: true.Why is it important to add a unique index in the database when using uniqueness validation?
✗ Incorrect
A unique index prevents duplicates even if two requests happen at the same time, which Rails validations alone cannot guarantee.
How do you make uniqueness validation ignore case differences?
✗ Incorrect
Use
case_sensitive: false option inside uniqueness validation to ignore case.What does the
scope option do in uniqueness validation?✗ Incorrect
The
scope option restricts uniqueness to records with the same value in the scoped attribute.If you want to validate uniqueness of email ignoring case and scoped to account_id, which is correct?
✗ Incorrect
You combine
case_sensitive: false and scope: :account_id inside uniqueness validation.Explain how to enforce uniqueness validation on a Rails model attribute and why adding a unique database index is important.
Think about both application-level and database-level protections.
You got /4 concepts.
Describe how to make uniqueness validation case insensitive and scoped to another attribute in Rails.
Combine options inside the uniqueness hash.
You got /3 concepts.