Challenge - 5 Problems
Length Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does this length validation behave?
Consider this Rails model validation:
What happens if you try to save a user with username 'Jo'?
validates :username, length: { minimum: 3, maximum: 8 }What happens if you try to save a user with username 'Jo'?
Attempts:
2 left
💡 Hint
Think about what minimum length means for a string.
✗ Incorrect
The validation requires the username to be at least 3 characters. 'Jo' has only 2 characters, so the save fails.
📝 Syntax
intermediate2:00remaining
Which length validation syntax is correct?
Which of these is the correct way to validate that a Rails model's attribute 'title' has a length exactly 5 characters?
Attempts:
2 left
💡 Hint
Look for the official Rails length validation option for exact length.
✗ Incorrect
The correct syntax uses the :is key inside the length hash to specify exact length.
❓ state_output
advanced2:00remaining
What error message appears for this validation?
Given this validation:
What error message will appear if password is '123'?
validates :password, length: { minimum: 6, too_short: 'must have at least %{count} characters' }What error message will appear if password is '123'?
Attempts:
2 left
💡 Hint
The custom message uses %{count} to show the minimum length.
✗ Incorrect
The error message uses the custom :too_short message and replaces %{count} with 6.
🔧 Debug
advanced2:00remaining
Why does this length validation not work as expected?
Look at this validation:
Why might the error message not show when the code is too short or too long?
validates :code, length: { minimum: 4, maximum: 10, message: 'length must be between 4 and 10' }Why might the error message not show when the code is too short or too long?
Attempts:
2 left
💡 Hint
Check how Rails handles error messages for length validations.
✗ Incorrect
Rails expects separate messages like :too_short and :too_long inside the length hash. A single :message is ignored for length.
🧠 Conceptual
expert2:00remaining
How does length validation interact with nil values?
Given this validation:
If the nickname is nil, what happens when saving the model?
validates :nickname, length: { maximum: 10 }If the nickname is nil, what happens when saving the model?
Attempts:
2 left
💡 Hint
Think about how Rails treats nil values in length validations by default.
✗ Incorrect
Length validation skips nil values unless :allow_nil is false. So nil passes validation.