0
0
Ruby on Railsframework~20 mins

Uniqueness validation in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Uniqueness Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a duplicate record is saved with uniqueness validation?
Given a Rails model with validates :email, uniqueness: true, what will happen if you try to save two records with the same email?
Ruby on Rails
class User < ApplicationRecord
  validates :email, uniqueness: true
end

user1 = User.create(email: 'test@example.com')
user2 = User.new(email: 'test@example.com')
success = user2.save
AThe second save will succeed but overwrite the first record silently.
BThe second save will succeed and create a duplicate record because uniqueness validation only works on update.
CThe second save will raise a runtime error due to a database constraint violation.
DThe second save will fail and <code>success</code> will be false because the email is not unique.
Attempts:
2 left
💡 Hint
Think about what the uniqueness validation does before saving a record.
📝 Syntax
intermediate
2:00remaining
Which option correctly adds case-insensitive uniqueness validation on username?
You want to ensure usernames are unique regardless of letter case. Which validation code is correct?
Ruby on Rails
class User < ApplicationRecord
  # Add validation here
end
Avalidates_uniqueness_of :username, case_sensitive: false
Bvalidates :username, uniqueness: { case_sensitive: false }
Cvalidates :username, uniqueness: true, case_sensitive: false
Dvalidates :username, unique: { case_sensitive: false }
Attempts:
2 left
💡 Hint
Look for the correct syntax for options inside the uniqueness validator.
🔧 Debug
advanced
3:00remaining
Why does uniqueness validation fail with concurrent record creation?
Consider this code:
class User < ApplicationRecord
  validates :email, uniqueness: true
end

# Two threads run:
User.create(email: 'a@example.com')
User.create(email: 'a@example.com')
Why might both records be created despite the validation?
ABecause uniqueness validation is not atomic and race conditions can allow duplicates before database constraints catch them.
BBecause Rails validations are skipped on create by default.
CBecause the validation only checks for uniqueness on update, not on create.
DBecause the database automatically allows duplicates unless a unique index is added.
Attempts:
2 left
💡 Hint
Think about what happens when two processes check uniqueness at the same time.
state_output
advanced
2:00remaining
What is the value of user.errors[:email] after a failed uniqueness validation?
Given this code:
class User < ApplicationRecord
  validates :email, uniqueness: true
end

user1 = User.create(email: 'dup@example.com')
user2 = User.new(email: 'dup@example.com')
user2.valid?
errors = user2.errors[:email]
What is the value of errors?
A["can't be blank"]
B[] (empty array)
C["has already been taken"]
D["is invalid"]
Attempts:
2 left
💡 Hint
What message does Rails add for uniqueness validation failures?
🧠 Conceptual
expert
3:00remaining
Why should you add a unique index in the database when using uniqueness validation?
Which is the best explanation for adding a unique index on a column that has Rails uniqueness validation?
ATo ensure data integrity at the database level and prevent duplicates even with concurrent writes.
BTo speed up the Rails validation process by caching results in the database.
CBecause Rails validations do not run on production environments by default.
DTo allow Rails to automatically rollback transactions on validation failure.
Attempts:
2 left
💡 Hint
Think about what happens if two users save the same data at the same time.