I'm using a before_save callback method to set an attribute to true if a number of other attributes are not 0. The callback is called when I update my model but the extra attribute isn't set. I think it might be because that attribute isn't passed to the update method. How do I work round that?
Controller
@blog.update(blog_params)
Model
before_save do
self.indicator = true unless attribute_1 == "0" && attribute_2 == "0"
end
You might consider using before_validation instead. Rails will call thebefore_validation callback before it calls before_save, so if you need to make sure you're setting a value, that would be the place to do so.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With