after_rollback on :create not triggered

41 views
Skip to first unread message

Cevad Tokatlı

unread,
May 20, 2019, 9:11:36 AM5/20/19
to Ruby on Rails: Talk

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


controller:

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


model:

  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.

Walter Lee Davis

unread,
May 20, 2019, 5:30:50 PM5/20/19
to rubyonra...@googlegroups.com
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).

Walter
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c0ad4f7b-d303-4fb8-8dc2-f72ea3517573%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages