module ActiveModel::Validations::HelperMethods
def validates_comparison_of(*attr_names)
validates_comparison_of :preferred_name, other_than: :given_name, allow_nil: true
validates_comparison_of :birth_date, less_than_or_equal_to: -> { Date.today }
class Person < ActiveRecord::Base
For example:
* :other_than
* :less_than_or_equal_to
* :less_than
* :equal_to
* :greater_than_or_equal_to
* :greater_than
Each will accept a proc, value, or a symbol which corresponds to a method:
The validator requires at least one of the following checks to be supplied.
See ActiveModel::Validations::ClassMethods#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:
other than %{count}"_.
supplied value. The default error message for this option is _"must be
* :other_than - Specifies the value must not be equal to the
this option is _"must be less than or equal to %{count}"_.
than or equal to the supplied value. The default error message for
* :less_than_or_equal_to - Specifies the value must be less
less than %{count}"_.
supplied value. The default error message for this option is _"must be
* :less_than - Specifies the value must be less than the
%{count}"_.
value. The default error message for this option is _"must be equal to
* :equal_to - Specifies the value must be equal to the supplied
for this option is _"must be greater than or equal to %{count}"_.
greater than or equal to the supplied value. The default error message
* :greater_than_or_equal_to - Specifies the value must be
greater than %{count}"_.
supplied value. The default error message for this option is _"must be
* :greater_than - Specifies the value must be greater than the
* :message - A custom error message (default is: "failed comparison").
Configuration options:
end
validates_comparison_of :value, greater_than: 'the sum of its parts'
class Person < ActiveRecord::Base
defined comparisons with another value, proc, or attribute.
Validates the value of a specified attribute fulfills all
def validates_comparison_of(*attr_names) validates_with ComparisonValidator, _merge_attributes(attr_names) end