Model Doesn’t Save After Adding before_save Callback

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:

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:

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”.

Posted in Rails.