Recall & Review
beginner
What is the purpose of numericality validation in Rails?
Numericality validation ensures that a model attribute contains only numbers, helping to keep data accurate and consistent.
Click to reveal answer
beginner
How do you add a basic numericality validation to a Rails model attribute?
Use
validates :attribute_name, numericality: true inside the model to check that the attribute is a number.Click to reveal answer
intermediate
What option would you use to allow only integers in numericality validation?
Add
only_integer: true like validates :age, numericality: { only_integer: true } to accept only whole numbers.Click to reveal answer
intermediate
How can you validate that a number is greater than or equal to 0 using numericality validation?
Use the
greater_than_or_equal_to: 0 option, for example: validates :price, numericality: { greater_than_or_equal_to: 0 }.Click to reveal answer
beginner
What happens if a non-numeric value is assigned to an attribute with numericality validation?
The model will be invalid and saving it will fail. Rails adds an error message to the attribute explaining it must be a number.
Click to reveal answer
Which validation ensures an attribute is a number in Rails?
✗ Incorrect
The numericality validation checks that the attribute contains a valid number.
How do you restrict a numericality validation to only allow integers?
✗ Incorrect
The correct option is only_integer: true to allow only whole numbers.
What option would you use to ensure a number is greater than 10?
✗ Incorrect
Use greater_than: 10 to require the number to be strictly more than 10.
If you want to allow nil values but validate numbers when present, what option do you add?
✗ Incorrect
allow_nil: true lets the attribute be empty but validates if a number is given.
What error message appears if a non-numeric value is assigned to a numericality validated attribute?
✗ Incorrect
Rails adds the error message "is not a number" for numericality validation failures.
Explain how to use numericality validation in a Rails model to accept only positive integers.
Think about combining options inside numericality.
You got /4 concepts.
Describe what happens when you try to save a Rails model with a non-numeric value on a numericality validated attribute.
Consider validation failure behavior.
You got /4 concepts.