Numericality validation checks if a value is a number. It helps keep data clean and correct.
Numericality validation in Ruby on Rails
validates :attribute_name, numericality: true
Replace :attribute_name with the name of the field you want to check.
You can add options like only_integer: true to allow only whole numbers.
age is a number (integer or float).validates :age, numericality: true
quantity is a whole number without decimals.validates :quantity, numericality: { only_integer: true }price is a number greater than zero.validates :price, numericality: { greater_than: 0 }score is a number not bigger than 100.validates :score, numericality: { less_than_or_equal_to: 100 }This example defines a Product model with two validations:
pricemust be a number greater than zero.stockmust be a whole number zero or more.
We create a product with invalid values to see the error messages.
class Product < ApplicationRecord validates :price, numericality: { greater_than: 0 } validates :stock, numericality: { only_integer: true, greater_than_or_equal_to: 0 } end product = Product.new(price: -5, stock: 3.5) product.valid? product.errors.full_messages
Numericality validation works only on attributes that respond to to_f or to_i.
Use allow_nil: true if you want to skip validation when the value is empty.
Validation error messages can be customized for better user feedback.
Numericality validation ensures values are numbers.
You can restrict to integers or set limits like greater than or less than.
This helps keep your data accurate and prevents mistakes.
Practice
validates :age, numericality: true line do in a Rails model?Solution
Step 1: Understand numericality validation
Thenumericality: trueoption checks if the attribute is a number.Step 2: Apply to
Since it is applied toageattributeage, it ensuresagemust be numeric.Final Answer:
It ensures theageattribute is a number. -> Option CQuick Check:
Numericality validation = ensures number [OK]
- Confusing numericality with presence validation
- Thinking it sets default values
- Assuming it checks for strings
price is an integer in Rails?Solution
Step 1: Recall correct numericality syntax
Rails usesnumericality: { only_integer: true }to check integers.Step 2: Check options
validates :price, numericality: { only_integer: true } matches this syntax exactly; others are invalid or incorrect.Final Answer:
validates :price, numericality: { only_integer: true } -> Option AQuick Check:
Integer validation uses only_integer: true [OK]
- Using a separate integer key instead of inside numericality
- Writing invalid keys like number or integer alone
- Missing curly braces for options
validates :score, numericality: { greater_than: 0, less_than_or_equal_to: 100 }What happens if you try to save
score = 150?Solution
Step 1: Understand validation limits
The validation requiresscoreto be greater than 0 and less than or equal to 100.Step 2: Check the value 150
150 is greater than 100, so it violates theless_than_or_equal_to: 100rule.Final Answer:
Validation fails because 150 is greater than 100. -> Option AQuick Check:
Value must be ≤ 100, 150 fails [OK]
- Ignoring upper limit and assuming number is valid
- Confusing greater_than with greater_than_or_equal_to
- Assuming any number passes
validates :quantity, numericality: { only_integer: true, greater_than: 0, less_than: 10, message: 'must be a number' }Solution
Step 1: Check placement of
Themessagemessageoption should be outside the numericality options hash, not inside it.Step 2: Validate other options
only_integer,greater_than, andless_thancan be combined correctly.Final Answer:
Themessageoption is misplaced inside the numericality hash. -> Option BQuick Check:
Message option goes outside numericality hash [OK]
- Placing message inside numericality hash
- Thinking only_integer conflicts with other options
- Assuming presence is required for numericality
rating attribute that must be an integer between 1 and 5 inclusive. Which validation code correctly enforces this?Solution
Step 1: Understand inclusive range requirements
The rating must include 1 and 5, so usegreater_than_or_equal_to: 1andless_than_or_equal_to: 5.Step 2: Check integer enforcement
only_integer: trueensures rating is an integer.Step 3: Compare options
validates :rating, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 5 } matches all requirements exactly; others exclude 1 or 5 or use wrong operators.Final Answer:
validates :rating, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 5 } -> Option DQuick Check:
Inclusive integer range uses >= and <= [OK]
- Using greater_than or less_than excludes boundary values
- Forgetting only_integer for integer check
- Mixing up operators for inclusive range
Ready to prove it?
Pass the test and this pattern moves unknown
