File this one under the Duh folder… I added a new before_save callback yesterday, and suddenly nothing in the model would save. Here is the offending code:
1 2 3 4 5 6 |
before_save :sms_verification def sms_verification if sms_number_changed? self.sms_number_verified = false end end |
Spot the problem? I didn’t. At least for a while.
In a callback, if you return false the save is cancelled. I knew that. So, when setting the sms_number_verified attribute to false , it also returns false from the callback, and therefore the model does not save. I did not expect that. Seems evident now that you see it.
Here is the fix:
1 2 3 4 5 6 7 |
before_save :sms_verification def sms_verification if sms_number_changed? self.sms_number_verified = false true end end |
Just return true from the callback.
Would have been nice if rails notified me in the logs with something like “save cancelled, before_save callback returned false on line XXX”.