save(:validate => false)涵盖了什么?

ssc*_*rus 5 ruby-on-rails counter-cache ruby-on-rails-3

我刚刚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
Run Code Online (Sandbox Code Playgroud)

我的问题是这个 - 命令Model.save(:validate => false)实际上阻​​止了什么,比如validates_with或者before_validation

如果我保留现有的保存而不进行验证,我的自定义counter_caches会受到影响吗?

Jes*_*ott 3

如果您传入 :validate=>false,它会跳过 valid? 命令。其他一切功能都相同。

您可以在此处查看代码: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
Run Code Online (Sandbox Code Playgroud)