* 更新:这已在 4.2.stable 和 4.2.1 中修复 *
在 Rails 4.2.0(和当前的 4.2.stable)中,该ensure_in_range
方法发生在 AR 验证之前,产生一个RangeError
如果我做一些简单的事情
@obj.threshold = 10_000_000_000
在具有 postgres 类型整数的列上
threshold | integer |
它产生
RangeError: 10000000000 is out of range for ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer with limit 4 from .../2.0.0-p598/lib/ruby/gems/2.0.0/bundler/gems/rails -62e9e61f2d1b/activerecord/lib/active_record/type/integer.rb:41:in `ensure_in_range'
这是真的!但告诉用户。有一个 ActiveRecord 模型验证,例如
validates :threshold, presence: true,
numericality: { greater_than_or_equal_to: 0, less_than: 1_000_000}
我无法想象这是预期的行为,任何人有任何解释为什么这种类型转换发生在验证之前?