[Feature request] ActiveModel::Validations::NumericalityValidator multiple_of? integration

Why not?

module ActiveModel
module Validations
class NumericalityValidator < EachValidator
CHECKS = { greater_than: :>, greater_than_or_equal_to: :>=,
equal_to: :==, less_than: :<, less_than_or_equal_to: :<=,
multiple_of: :multiple_of?, odd: :odd?, even: :even?,
odd: :odd?, even: :even?, other_than: :!= }.freeze




Where did that method come from? All other methods are Ruby methods.

Sorry, I’ve seen only now that it is an ActiveSupport extension of Integer.

File activesupport/lib/active_support/core_ext/integer/multiple.rb, line 7

def multiple_of?(number)

number != 0 ? self % number == 0 : zero?



multiple_of isn’t a supported option for NumericalityValidator because the method only exists for integers. Here’s some previous discussion of this situation:

https://github.com/rails/rails/pull/7216 (on why multiple_of isn’t defined for all of Numeric)

https://github.com/rails/rails/pull/7213 (on adding this exact feature)

TL;DR the core of the problem is that % is a tricky operator for floating-point numbers. 1.0 % 0.1, for instance, is usually NOT 0.0 like you’d expect…

—Matt Jones

It makes sense. Thanks.