0
0
Ruby on Railsframework~10 mins

Numericality validation in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Numericality validation
Start: Model with attribute
Apply numericality validation
Input value assigned
Check if value is a number
Pass
Save success
End
This flow shows how Rails checks if a model attribute is a number during validation, allowing save if valid or adding errors if not.
Execution Sample
Ruby on Rails
class Product < ApplicationRecord
  validates :price, numericality: true
end

product = Product.new(price: 'abc')
product.valid?
This code checks if the price attribute is a number; here it tests with a non-numeric string.
Execution Table
StepActionInput ValueValidation CheckResultError Added
1Assign price'abc'N/AValue setNo
2Run numericality validation'abc'Is 'abc' a number?NoYes: 'price is not a number'
3Check product validityN/AAre there errors?YesYes
4Attempt saveN/AValid? FalseSave failsYes
💡 Validation fails because 'abc' is not a number, so save is prevented.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
product.pricenil'abc''abc''abc''abc'
product.errors[:price][][]['is not a number']['is not a number']['is not a number']
product.valid?nilnilfalsefalsefalse
Key Moments - 3 Insights
Why does the validation fail when price is 'abc'?
Because 'abc' is not a number, the numericality check returns false and adds an error (see execution_table step 2).
What happens if price is a number like 10?
The validation passes, no errors are added, and the product can be saved successfully (not shown here but implied).
Does numericality validation allow strings that look like numbers?
Yes, strings like '10' are valid numbers and pass validation, but non-numeric strings like 'abc' fail.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what error message is added when price is 'abc'?
A'price must be greater than zero'
B'price can't be blank'
C'price is not a number'
DNo error message
💡 Hint
Check the 'Error Added' column in step 2 of the execution_table.
At which step does the product become invalid due to numericality validation?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Validation Check' and 'Result' columns in the execution_table.
If price was set to 25 instead of 'abc', how would the 'product.valid?' value change after step 3?
AIt would be false
BIt would be true
CIt would be nil
DIt would cause an error
💡 Hint
Refer to variable_tracker for 'product.valid?' and consider a valid numeric input.
Concept Snapshot
Numericality validation in Rails checks if an attribute is a number.
Use: validates :attribute, numericality: true
If the value is not numeric, validation fails and errors are added.
Valid numeric strings like '10' pass; non-numeric like 'abc' fail.
This prevents saving invalid data to the database.
Full Transcript
Numericality validation in Rails ensures a model attribute holds a number. When you assign a value to the attribute, Rails checks if it is numeric during validation. If the value is not a number, Rails adds an error message like 'is not a number' and prevents saving the record. For example, if you set price to 'abc', validation fails. But if you set price to 10 or '10', validation passes. This helps keep data clean and correct in your app.