Fix issue #8937 after_commit(on: :update) executed erroneously

We check for a state in the current ActiveRecord
transaction that causes the wrong callbacks to be
executed, and when it occurs, call the correct
callbacks.

Tests lifted from
https://github.com/rails/rails/commit/b42ea033115c1a06160e4092039d6a0fe0bda6f8