validates_comparison_of
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0 (0)
- 7.1.3.2 (38)
- 7.1.3.4 (0)
- What's this?
validates_comparison_of(*attr_names)
public
Validates the value of a specified attribute fulfills all defined comparisons with another value, proc, or attribute.
class Person < ActiveRecord::Base validates_comparison_of :value, greater_than: 'the sum of its parts' end
Configuration options:
-
:message - A custom error message (default is: “failed comparison”).
-
:greater_than - Specifies the value must be greater than the supplied value. The default error message for this option is _“must be greater than %{count}”_.
-
:greater_than_or_equal_to - Specifies the value must be greater than or equal to the supplied value. The default error message for this option is _“must be greater than or equal to %{count}”_.
-
:equal_to - Specifies the value must be equal to the supplied value. The default error message for this option is _“must be equal to %{count}”_.
-
:less_than - Specifies the value must be less than the supplied value. The default error message for this option is _“must be less than %{count}”_.
-
:less_than_or_equal_to - Specifies the value must be less than or equal to the supplied value. The default error message for this option is _“must be less than or equal to %{count}”_.
-
:other_than - Specifies the value must not be equal to the supplied value. The default error message for this option is _“must be other than %{count}”_.
There is also a list of default options supported by every validator: :if, :unless, :on, :allow_nil, :allow_blank, and :strict . See ActiveModel::Validations::ClassMethods#validates for more information.
The validator requires at least one of the following checks to be supplied. Each will accept a proc, value, or a symbol which corresponds to a method:
-
:greater_than
-
:greater_than_or_equal_to
-
:equal_to
-
:less_than
-
:less_than_or_equal_to
-
:other_than
For example:
class Person < ActiveRecord::Base validates_comparison_of :birth_date, less_than_or_equal_to: -> { Date.today } validates_comparison_of :preferred_name, other_than: :given_name, allow_nil: true end