Rails model validation on create and update only

匿名 (未验证) 提交于 2019-12-03 01:55:01

问题:

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?

回答1:

By default, the validations run for both create and update. So it should be just:

validates_presence_of :password 

The :on key just allows you to choose one of them.



回答2:

Only write:

validates_presence_of :password 

No need...

on => :create 


回答3:

You can use this when you need to disable the validation on some specific operations, such as delete.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!