Challenge - 5 Problems
Validation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
Why use validations in Rails models?
Which statement best explains why validations are important in Rails models?
Attempts:
2 left
💡 Hint
Think about what happens if bad data is saved without checks.
✗ Incorrect
Validations check data before saving to keep the database clean and consistent. Without them, invalid or incomplete data could cause bugs or crashes.
❓ component_behavior
intermediate1:30remaining
What happens when a validation fails?
In Rails, if a model validation fails when saving a record, what is the expected behavior?
Attempts:
2 left
💡 Hint
Consider how Rails helps you handle invalid data gracefully.
✗ Incorrect
When validations fail, Rails prevents saving and stores error messages so you can inform users what went wrong.
📝 Syntax
advanced2:00remaining
Identify the correct validation syntax
Which option correctly validates that a User model's email attribute is present and unique in Rails?
Ruby on Rails
class User < ApplicationRecord # validation here end
Attempts:
2 left
💡 Hint
Look for the correct method and option names.
✗ Incorrect
The validates method with presence: true and uniqueness: true is the correct syntax for these validations.
❓ state_output
advanced2:00remaining
What is the state of the model after failed validation?
Given this code snippet, what will be the value of
user.errors[:name] after user.save if the name is blank?
user = User.new(name: "") success = user.save
Attempts:
2 left
💡 Hint
Think about what Rails adds to errors when presence validation fails.
✗ Incorrect
When presence validation fails, Rails adds the message "can't be blank" to the errors for that attribute.
🔧 Debug
expert2:30remaining
Why does this validation not prevent saving invalid data?
Consider this User model:
class User < ApplicationRecord
validates :age, numericality: { greater_than: 18 }
end
user = User.new(age: 17)
user.save(validate: false)
Why does the user get saved even though age is less than 18?Attempts:
2 left
💡 Hint
Look closely at the save method call.
✗ Incorrect
Calling save(validate: false) tells Rails to skip validations, so invalid data can be saved.