Rails 4/ 验证 - 数值:{ only_integer: true } 'integer' 属性是否必要?
Rails 4/ validations - numericality: { only_integer: true } necessary on 'integer' attribute?
我的模型上有这个:
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.
正如您在上面所读到的,它说“......事实上,您可以从模型中删除验证,因为它也没有做任何事情。”
我应该在 'integer' 数据库表的列上使用数值:{ only_integer: true } 吗?
验证助手 numericality
适用于 String
值(来自 Postgres 的 VARCHAR
),它会将它们与正则表达式匹配。
因此,您说得对,不是 数据库中本机 Integer
值所必需的。
我的模型上有这个:
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.
正如您在上面所读到的,它说“......事实上,您可以从模型中删除验证,因为它也没有做任何事情。”
我应该在 'integer' 数据库表的列上使用数值:{ only_integer: true } 吗?
验证助手 numericality
适用于 String
值(来自 Postgres 的 VARCHAR
),它会将它们与正则表达式匹配。
因此,您说得对,不是 数据库中本机 Integer
值所必需的。