Recall & Review
beginner
What is length validation in Rails?
Length validation checks if the size of a string or array attribute is within a specified range or matches a specific length.
Click to reveal answer
beginner
How do you validate that a username is at least 3 characters long in Rails?
Use
validates :username, length: { minimum: 3 } inside the model.Click to reveal answer
intermediate
What options can you use with length validation in Rails?
You can use
minimum, maximum, is (exact length), and within or in (range).Click to reveal answer
beginner
What happens if a length validation fails when saving a record?
The record is not saved, and errors are added to the model's
errors collection for that attribute.Click to reveal answer
beginner
Show an example of validating a password length between 6 and 12 characters in Rails.
In the model:
validates :password, length: { within: 6..12 }Click to reveal answer
Which option validates that a string is exactly 5 characters long in Rails?
✗ Incorrect
The
is option checks for exact length.What does
validates :name, length: { minimum: 2 } ensure?✗ Incorrect
The
minimum option sets the lowest allowed length.If a length validation fails, what happens when you try to save the record?
✗ Incorrect
Rails prevents saving and adds errors to the model.
Which length validation option allows specifying a range of valid lengths?
✗ Incorrect
The
within option accepts a range like 3..10.How do you write a length validation for a field to be between 4 and 8 characters?
✗ Incorrect
You can use either
within: 4..8 or minimum: 4, maximum: 8.Explain how to use length validation in a Rails model and what options are available.
Think about how you tell Rails to check string sizes.
You got /4 concepts.
Describe what happens behind the scenes when a length validation fails during record saving.
Consider how Rails protects data integrity.
You got /4 concepts.