Mar*_*son 3 activerecord attributes ruby-on-rails callback
如果许多其他属性不是0,我正在使用before_save回调方法将属性设置为true.当我更新模型但未设置额外属性时,将调用回调.我认为可能是因为该属性未传递给update方法.我该如何解决这个问题呢?
调节器
@blog.update(blog_params)
Run Code Online (Sandbox Code Playgroud)
模型
before_save do
self.indicator = true unless attribute_1 == "0" && attribute_2 == "0"
end
Run Code Online (Sandbox Code Playgroud)
你可以考虑before_validation改用.Rails会在before_validation调用之前调用回调before_save函数,因此如果您需要确保设置一个值,那么这就是这样做的地方.