before_* callbacks rollback transactions if its method returns false
Public, Rails Errors, Troubleshooting
Created: Oct 05, 2016 Updated: Oct 05, 2016
I had a form, where in one of the fields, the user was clicking a drop-down menu, and choosing between one of three choices.
After the form was submitted, I had a callback that was doing something to the record.
When the user selected option 2, the callback worked; however, if they selected either option 1 or 3, the record kept rolling back.
After trying numerous debugging approaches in the controller, in the model, in the form, and more, nothing revealed why these two selections caused a rollback, while the third selection always worked.
After consulting a colleague on the issue, and both of us being baffled for quite some time, he ultimately uncovered that:
before_*callbacks rollback transactions if its method returns
Let's take a look at the callback, before and after this revelation.
Initially I had the following
self.accrues_pto = false line and the
self.accrues_pto = false line was returning
false, thereby triggering the
before_save callback to rollback the record.
After consulting with another team member, he advised us that:
It's a good practice to always explicitly
returnfrom callbacks (if not intending to rollback), to avoid encountering these head-scratchers.
Based on that advice, here is the updated callback method, which works as desired:
You'll note the explicit
return at the end of the method.
This same teammate then went onto to advise us that:
The only significant return value of callbacks is
false(Rails will cancel the rest of the callbacks). Other values returned from callbacks, including
nil(which is returned when you just do
return) don't trigger any special processing.