save(:validate => false) 涵盖了什么? [英] What is covered by save(:validate => false)?
问题描述
我刚刚使用这样的代码实现了一些自定义的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
我的问题是 - 命令 Model.save(:validate => false)
实际上阻止了诸如 validates_with
或 before_validation之类的事情代码>?
My question is this - what does the command Model.save(:validate => false)
actually prevent beyond things like validates_with
or before_validation
?
如果我保留现有保存而不进行验证,我的自定义 counter_caches 会受到影响吗?
Will my custom counter_caches be affected if I keep my existing saves without validation?
推荐答案
如果传入 :validate=>false,它会跳过有效的?命令.其他功能都一样.
If you pass in the :validate=>false, it skips the valid? command. Everything else functions the same.
您可以在此处查看代码:http://api.rubyonrails.org/类/ActiveRecord/Validations.html
You can check the code out here: http://api.rubyonrails.org/classes/ActiveRecord/Validations.html
def save(options={})
perform_validations(options) ? super : false
end
...
if perform_validation
valid?(options.is_a?(Hash) ? options[:context] : nil)
else
true
end
这篇关于save(:validate => false) 涵盖了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!