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
类的东西?
如果我保留现有的保存而不进行validation,我的自定义counter_caches会受到影响吗?
如果你传入:validate => false,它会跳过有效的? 命令。 其他一切function相同。
您可以在此处查看代码: 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
在Rails 4.2.6上测试表明.save(:validate=>false)
实际上会跳过before_validations
和after_validation
回调。