仅在创建和更新时验证 Rails 模型 [英] Rails model validation on create and update only
本文介绍了仅在创建和更新时验证 Rails 模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我只想在创建时进行验证,那么我可以这样做
If I want to have validation only on create, then I can do
validates_presence_of :password, :on => :create
但是我如何说创建和更新?我试过了,但没有用:
But how do I say on create and update? I tried this but it didn't work:
validates_presence_of :password, :on => [ :create, :update ]
我是否必须定义两次验证?
Do I have to define the validation two times?
推荐答案
默认情况下,验证会针对创建和更新运行.所以它应该只是:
By default, the validations run for both create and update. So it should be just:
validates_presence_of :password
:on 键只允许您选择其中之一.
The :on key just allows you to choose one of them.
这篇关于仅在创建和更新时验证 Rails 模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文