Bird
Raised Fist0
Ruby on Railsframework~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Length Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
How does this length validation behave?
Consider this Rails model validation:

validates :username, length: { minimum: 3, maximum: 8 }

What happens if you try to save a user with username 'Jo'?
AThe save fails because 'Jo' is longer than the maximum length 8.
BThe save succeeds because 'Jo' is a valid string.
CThe save succeeds but triggers a warning.
DThe save fails because 'Jo' is shorter than the minimum length 3.
Attempts:
2 left
💡 Hint
Think about what minimum length means for a string.
📝 Syntax
intermediate
2: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?
Avalidates :title, length: { min: 5, max: 5 }
Bvalidates_length_of :title, equal_to: 5
Cvalidates :title, length: { is: 5 }
Dvalidates :title, length: 5
Attempts:
2 left
💡 Hint
Look for the official Rails length validation option for exact length.
❓ state_output
advanced
2:00remaining
What error message appears for this validation?
Given this validation:

validates :password, length: { minimum: 6, too_short: 'must have at least %{count} characters' }

What error message will appear if password is '123'?
A"Password is invalid"
B"must have at least 6 characters"
C"Password is too short (minimum is 6 characters)"
D"Password must be at least 3 characters"
Attempts:
2 left
💡 Hint
The custom message uses %{count} to show the minimum length.
🔧 Debug
advanced
2:00remaining
Why does this length validation not work as expected?
Look at this validation:

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?
AThe :message option applies to all length errors, but Rails expects separate messages for minimum and maximum.
BThe :message option is ignored because it must be outside the length hash.
CThe validation syntax is invalid and causes a syntax error.
DThe attribute :code is missing from the model.
Attempts:
2 left
💡 Hint
Check how Rails handles error messages for length validations.
🧠 Conceptual
expert
2:00remaining
How does length validation interact with nil values?
Given this validation:

validates :nickname, length: { maximum: 10 }

If the nickname is nil, what happens when saving the model?
AThe validation passes because length validation ignores nil values by default.
BThe validation fails because nil has no length.
CThe validation raises a runtime error.
DThe validation passes only if :allow_nil is set to true.
Attempts:
2 left
💡 Hint
Think about how Rails treats nil values in length validations by default.

Practice

(1/5)
1. What does the length validation in Rails do?
easy
A. It checks how many characters a text field contains.
B. It validates the numerical value of a field.
C. It ensures a field is not empty.
D. It checks if a field contains only letters.

Solution

  1. Step 1: Understand the purpose of length validation

    Length validation controls the number of characters allowed in a text field.
  2. Step 2: Compare options with this purpose

    Only It checks how many characters a text field contains. matches this description exactly.
  3. Final Answer:

    It checks how many characters a text field contains. -> Option A
  4. Quick Check:

    Length validation = character count check [OK]
Hint: Length validation controls character count in text fields [OK]
Common Mistakes:
  • Confusing length with numerical value validation
  • Thinking it checks for empty fields
  • Assuming it validates character types
2. Which of the following is the correct syntax to validate that a username has a minimum length of 5 characters in Rails?
easy
A. validates :username, length: minimum: 5
B. validates :username, length: { minimum: 5 }
C. validates_length_of :username, min: 5
D. validates :username, length => { min: 5 }

Solution

  1. Step 1: Recall correct Rails length validation syntax

    The correct syntax uses validates :field, length: { minimum: value }.
  2. Step 2: Check each option for syntax correctness

    validates :username, length: { minimum: 5 } matches the correct syntax exactly; others have syntax errors or wrong keys.
  3. Final Answer:

    validates :username, length: { minimum: 5 } -> Option B
  4. Quick Check:

    Minimum length syntax = validates :field, length: { minimum: x } [OK]
Hint: Use curly braces and 'minimum' key inside length hash [OK]
Common Mistakes:
  • Omitting curly braces around length options
  • Using wrong keys like 'min' instead of 'minimum'
  • Incorrect hash rocket syntax
3. Given the model validation:
validates :title, length: { is: 10 }

What will happen if you try to save a record with title = 'Short'?
medium
A. The record will save successfully.
B. The record will fail validation because the title is too long.
C. The record will fail validation because the title is too short.
D. There will be a syntax error.

Solution

  1. Step 1: Understand the 'is' option in length validation

    The 'is' option requires the field to be exactly the specified length (10 characters here).
  2. Step 2: Check the length of 'Short'

    'Short' has 5 characters, which is less than 10, so it fails validation for being too short.
  3. Final Answer:

    The record will fail validation because the title is too short. -> Option C
  4. Quick Check:

    Length 'is' means exact length required [OK]
Hint: 'is' means exact length, not minimum or maximum [OK]
Common Mistakes:
  • Thinking 'is' means minimum length
  • Assuming shorter strings pass validation
  • Confusing syntax error with validation failure
4. Identify the error in this validation:
validates :description, length: { max: 100 }
medium
A. The key 'max' should be 'maximum'.
B. The key 'max' should be 'min'.
C. The validation should use 'validates_length_of' instead.
D. There is no error; this is correct syntax.

Solution

  1. Step 1: Recall correct keys for length validation

    Rails uses 'minimum', 'maximum', and 'is' keys inside length hash.
  2. Step 2: Check the key used in the code

    The code uses 'max', which is incorrect; it should be 'maximum'.
  3. Final Answer:

    The key 'max' should be 'maximum'. -> Option A
  4. Quick Check:

    Use 'maximum' not 'max' in length validation [OK]
Hint: Use 'maximum' not 'max' for max length validation [OK]
Common Mistakes:
  • Using 'max' instead of 'maximum'
  • Confusing 'min' and 'maximum'
  • Thinking 'validates_length_of' is required
5. You want to validate a password field so it must be exactly 8 characters long and not empty. Which validation code correctly enforces this?
hard
A. validates :password, length: { minimum: 8, presence: true }
B. validates :password, length: { minimum: 8 }
C. validates :password, length: 8, presence: true
D. validates :password, length: { is: 8 }, presence: true

Solution

  1. Step 1: Understand how to enforce exact length and presence

    Use length: { is: 8 } for exact length and presence: true to ensure not empty.
  2. Step 2: Evaluate each option

    validates :password, length: { is: 8 }, presence: true correctly combines exact length and presence validations. validates :password, length: { minimum: 8 } misses presence. validates :password, length: 8, presence: true has wrong length syntax. validates :password, length: { minimum: 8, presence: true } incorrectly nests presence inside length.
  3. Final Answer:

    validates :password, length: { is: 8 }, presence: true -> Option D
  4. Quick Check:

    Exact length + presence = separate keys [OK]
Hint: Use separate keys for length and presence validations [OK]
Common Mistakes:
  • Putting presence inside length hash
  • Using length: 8 instead of length: { is: 8 }
  • Omitting presence validation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard