简体   繁体   English

save(false)和save(:validate => false)之间的区别

[英]Difference between save(false) and save(:validate => false)

What's the difference between save(false) and save(:validate => false) ? save(false)save(:validate => false)之间有什么区别? From what I can tell, the functionality is the same. 据我所知,功能是一样的。 The version that uses :validate is in the api which leads me to believe save(false) is a deprecated version? 使用的版本:validate是在api中让我相信save(false)是一个弃用的版本? This came up for me when following this: https://github.com/plataformatec/devise/wiki/How-To:-Create-a-guest-user . 在我这样做时,我想到了这个: https//github.com/plataformatec/devise/wiki/How-To : -Create-a-guest-user The guide has save(false) in there but I was getting errors when using it. 该指南已save(false) ,但在使用时遇到错误。 I switched it to the :validate version and that worked fine. 我把它切换到:validate版本,并且工作正常。

In Rails versions before than 3, save was a method in ActiveRecord::Base and you could pass false to it in order to bypass validations. 在3之前的Rails版本中, save ActiveRecord::Base 一个方法 ,您可以将false传递给它以绕过验证。

In Rails 3, save was moved to ActiveRecord::Persistance and since then you should pass :validate => false to save in order to bypass validations. 在Rails 3中, save 被移动ActiveRecord::Persistance ,从那时起你应该传递:validate => falsesave以绕过验证。

All the validation from model are skipped when we use validate: false 当我们使用validate: false时,将跳过模型的所有验证

@user = User.new(....)

@user.save(validate: false)

Action base disable validation 动作基地禁用验证

http://www.dan-manges.com/blog/action-dependent-validations-and-why-on-update-is-bad http://www.dan-manges.com/blog/action-dependent-validations-and-why-on-update-is-bad

Skip Field validation 跳过字段验证

https://richonrails.com/articles/skipping-validations-in-ruby-on-rails https://richonrails.com/articles/skipping-validations-in-ruby-on-rails

Example

class User < ActiveRecord::Base class User <ActiveRecord :: Base

validates_presence_of :password, :on => :update validates_presence_of:password,:on =>:update

end 结束

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM