How to Use Numericality Validation in Ruby on Rails
In Ruby on Rails, use
validates :attribute, numericality: true in your model to ensure a field contains only numbers. You can customize it with options like only_integer: true or greater_than to enforce specific numeric rules.Syntax
The numericality validation checks if a model attribute is a number. You write it inside your model like this:
validates :attribute_name, numericality: true- ensures the value is numeric.- Options include:
only_integer: true- value must be an integer.greater_than: number- value must be greater than given number.less_than_or_equal_to: number- value must be less than or equal to given number.allow_nil: true- skips validation if value is nil.
ruby
class Product < ApplicationRecord validates :price, numericality: { only_integer: false, greater_than: 0 } end
Example
This example shows a Product model where the price must be a number greater than zero and can have decimals. It also validates stock to be an integer greater than or equal to zero.
ruby
class Product < ApplicationRecord validates :price, numericality: { greater_than: 0 } validates :stock, numericality: { only_integer: true, greater_than_or_equal_to: 0 } end # Usage example in Rails console: product = Product.new(price: 10.5, stock: 5) product.valid? # => true product2 = Product.new(price: -3, stock: 2.5) product2.valid? # => false product2.errors.full_messages # => ["Price must be greater than 0", "Stock must be an integer"]
Output
true
false
["Price must be greater than 0", "Stock must be an integer"]
Common Pitfalls
Common mistakes include:
- Using
numericality: truewithout options when you need integers only. - Not allowing
nilvalues when the attribute can be empty, causing unexpected validation failures. - Confusing string numbers with actual numbers; Rails converts strings like "123" to numbers automatically.
Example of wrong and right usage:
ruby
# Wrong: allows decimals but you want only integers validates :age, numericality: true # Right: enforce integer only validates :age, numericality: { only_integer: true } # Wrong: validation fails if value is nil validates :score, numericality: true # Right: allow nil values validates :score, numericality: true, allow_nil: true
Quick Reference
Here is a quick summary of common numericality options:
| Option | Description |
|---|---|
| numericality: true | Validates attribute is a number (integer or float) |
| only_integer: true | Value must be an integer |
| greater_than: number | Value must be greater than given number |
| greater_than_or_equal_to: number | Value must be greater than or equal to given number |
| less_than: number | Value must be less than given number |
| less_than_or_equal_to: number | Value must be less than or equal to given number |
| equal_to: number | Value must be exactly equal to given number |
| allow_nil: true | Skip validation if value is nil |
| allow_blank: true | Skip validation if value is blank (nil or empty string) |
Key Takeaways
Use
validates :attribute, numericality: true to ensure a value is numeric in Rails models.Add options like
only_integer: true or greater_than to customize numeric rules.Remember to use
allow_nil: true if the attribute can be empty to avoid validation errors.Rails automatically converts numeric strings to numbers during validation.
Check validation errors with
errors.full_messages to understand why a record is invalid.