即使存在未设置为 true,Rails 验证也需要数字 [英] Rails validation required numericality even though presence is not set to true

查看:46
本文介绍了即使存在未设置为 true,Rails 验证也需要数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存没有一个字段集的记录——它在模型中具有验证数字.即使验证中不需要存在,它仍然会抛出该字段不是数字的错误.

I'm trying to save a record which doesn't have one field set -- which has a validate numericality in the models. Even though the presence is not required in the validation, it's still throwing an error that the field is not a number.

验证:

validates :network_id,    :numericality => true

保存模型的代码:

networks.each do |network|
  network.url = network.raw_data.link
  network.save!
end

错误:

Validation failed: Network is not a number

推荐答案

validates :network_id, :numericality => true, :allow_nil => true

这篇关于即使存在未设置为 true,Rails 验证也需要数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆