module ActiveModel::Validations::HelperMethods
def validates_numericality_of(*attr_names)
validates_numericality_of :width, greater_than: :minimum_weight
validates_numericality_of :width, less_than: ->(person) { person.height }
class Person < ActiveRecord::Base
For example:
* :only_integer
* :less_than_or_equal_to
* :less_than
* :equal_to
* :greater_than_or_equal_to
* :greater_than
corresponds to a method:
The following checks can also be supplied with a proc or a symbol which
See ActiveModel::Validations#validates for more information
+:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+ .
There is also a list of default options supported by every validator:
* :even - Specifies the value must be an even number.
* :odd - Specifies the value must be an odd number.
supplied value.
* :other_than - Specifies the value must be other than the
than or equal the supplied value.
* :less_than_or_equal_to - Specifies the value must be less
supplied value.
* :less_than - Specifies the value must be less than the
value.
* :equal_to - Specifies the value must be equal to the supplied
greater than or equal the supplied value.
* :greater_than_or_equal_to - Specifies the value must be
supplied value.
* :greater_than - Specifies the value must be greater than the
converted to +nil+.
+false+). Notice that for Integer and Float columns empty strings are
* :allow_nil - Skip validation if attribute is +nil+ (default is
integer, e.g. an integral value (default is +false+).
* :only_integer - Specifies whether the value has to be an
* :message - A custom error message (default is: "is not a number").
Configuration options:
end
validates_numericality_of :value, on: :create
class Person < ActiveRecord::Base
(if only_integer is set to +true+).
is +false+) or applying it to the regular expression /\A[\+\-]?\d+\Z/
trying to convert it to a float with Kernel.Float (if only_integer
Validates whether the value of the specified attribute is numeric by
def validates_numericality_of(*attr_names) validates_with NumericalityValidator, _merge_attributes(attr_names) end