Rails 4 /验证 - 数字:{only_integer:true}必须在'整数'属性?

时间:2015-10-25 00:05:54

标签: ruby-on-rails postgresql ruby-on-rails-4 rspec shoulda

我在模特身上有这个:

validates :deal_sector_id,
              presence: true,
              numericality: { only_integer: true }

deal_sector_id是一个整数postgresql列。

我开始怀疑数字性:{only_integer:true}在我的验证中是必要的,因为当我升级到shoulda-matchers 3.0时,我收到此错误消息:

You are attempting to use validate_numericality_of, but the attribute
       you're testing, :deal_sector_id, is an integer column. One of the
       things that the numericality matcher does is to assert that setting
       :deal_sector_id to a string that doesn't look like an integer will
       cause your deal to become invalid. In this case, it's impossible to
       make this assertion since :deal_sector_id will typecast any incoming
       value to an integer. This means that it's already guaranteed to be
       numeric! Since this matcher isn't doing anything, you can remove it
       from your model tests, and in fact, you can remove the validation from
       your model as it isn't doing anything either.

如上所述,它表示" ...事实上,您可以从模型中删除验证,因为它不会做任何事情。"

我应该使用数字:{only_integer:true} on'整数'数据库表'列?

1 个答案:

答案 0 :(得分:1)

验证助手numericality适用于String值(Postgres的VARCHAR),并且会将它们与正则表达式进行匹配。

因此,您说来自数据库的本地Integer 是正确的。