0
0
Ruby on Railsframework~5 mins

Numericality validation in Ruby on Rails - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Apresence
Bnumericality
Cuniqueness
Dlength
How do you restrict a numericality validation to only allow integers?
Ais_integer: true
Binteger_only: true
Cinteger: true
Donly_integer: true
What option would you use to ensure a number is greater than 10?
Agreater_than: 10
Bmin: 10
Cabove: 10
Dgreater_or_equal: 10
If you want to allow nil values but validate numbers when present, what option do you add?
Askip_nil: true
Boptional: true
Callow_nil: true
Dignore_nil: true
What error message appears if a non-numeric value is assigned to a numericality validated attribute?
A"is not a number"
B"can't be blank"
C"is invalid"
D"must be unique"
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.