after_rollback on :create not triggered

I have a product table and I try to run after_commit and after_rollback on create.


  product = Product.  new form_params
if product.    save
# code
    # code


  after_commit(on: :create) {
    # code

  after_rollback(on: :create) {
    # code

after_commit works well but after_rollback wont work with on: :create param. When I remove the on: :create param, it triggers too but I need after_rollback to trigger just on create.

It seems possible that the on: attribute is not referenced in the after_rollback callback. Is it listed in the documentation?

You could probably get the same result by adding a test inside your method:

after_rollback {
  return if persisted?
  // whatever else here

This will only ever proceed beyond that checkpoint if the record was in the process of being created, since on an update or delete, the record would be persisted (have an ID).