save(:validate => false) 涵盖了什么?
我刚刚使用如下代码实现了许多自定义 counter_cache
:
def after_save
self.update_counter_cache
end
def after_destroy
self.update_counter_cache
end
def update_counter_cache
self.company.new_matchings_count = Matching.where(:read => false).count
self.company.save
end
我的问题是 - 命令 Model.save(:validate => false)
实际上阻止了什么除了 validates_with
或 before_validation
之类的东西之外?
如果我保留现有的保存而不进行验证,我的自定义 counter_cache 是否会受到影响?
I just implemented a number of custom counter_cache
s using code like this:
def after_save
self.update_counter_cache
end
def after_destroy
self.update_counter_cache
end
def update_counter_cache
self.company.new_matchings_count = Matching.where(:read => false).count
self.company.save
end
My question is this - what does the command Model.save(:validate => false)
actually prevent beyond things like validates_with
or before_validation
?
Will my custom counter_caches be affected if I keep my existing saves without validation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Rails 4.2.6 上的测试表明
.save(:validate=>false)
实际上会跳过before_validations
和after_validation
回调。Testing on Rails 4.2.6 shows that
.save(:validate=>false)
will actually skipbefore_validations
andafter_validation
callbacks.如果您传入 :validate=>false,它会跳过 valid?命令。其他一切功能都相同。
您可以在此处查看代码: http://api.rubyonrails.org/classes/ActiveRecord /Validations.html
If you pass in the :validate=>false, it skips the valid? command. Everything else functions the same.
You can check the code out here: http://api.rubyonrails.org/classes/ActiveRecord/Validations.html