本文介绍了引发RangeError在Rails的4.2.0简单的整数分配应抓住了验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

*更新:这是现在固定在4.2.stable和4.2.1 *

在Rails的4.2.0(和目前4.2.stable),在 ensure_in_range 方法AR验证之前发生,产生了的RangeError

in Rails 4.2.0 (and current 4.2.stable), the ensure_in_range method happens before AR validation, yielding a RangeError

如果我做一些简单的

@obj.threshold = 10_000_000_000

这是一个Postgres类型的整数列

on a column with a postgres type integer

 threshold  | integer                     | 

它产生

的RangeError:百亿超出范围的ActiveRecord :: ConnectionAdapters :: PostgreSQL的OID :: ::整数与限制4  从.../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

这是真的!但告诉给用户。还有像

which is true! but tell that to the users. there's an ActiveRecord model validation like

  validates :threshold,  presence: true,
    numericality: { greater_than_or_equal_to: 0, less_than: 1_000_000}

我无法想象这是预期的行为,任何人有任何的解释,为什么这种类型的投验证前,会发生什么?

i can't imagine this is expected behavior, anyone have any explanation why this type cast happens before validation?

推荐答案

获取最新版本的轨道修正这个错误,它最近固定肖恩·格里芬

Get the latest rails version to fix this error, it was recently fixed by Sean Griffin

要做到这一点的一个版本出来之前,删除特定版本在你的Gemfile和使用Git的位置提示:

To do that before a version comes out, remove the specific version in your gemfile and use the git location hint :

gem 'rails', :git => 'https://github.com/rails/rails.git'

这篇关于引发RangeError在Rails的4.2.0简单的整数分配应抓住了验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 13:09