ruby-on-railscallbackruby-on-rails-7rails7

unexpected callbacks behaviour in rails 7


I've been trying to migrate my codebase from rails 5 to rails 7 and there are some issues with callbacks which I'm unable to figure out

Issues:

  1. When multiple callbacks like after_create_commit and after_update_commit are supposed to trigger the same method say foo the method doesn't get called. For example:
after_create_commit :foo
after_update_commit :foo

But it does when I slightly change the syntax as

after_create_commit -> { foo }
after_update_commit -> { foo }
  1. If there are multiple methods to be triggered after a callback then only the last one gets called and all the ones before it doesn't get executed, For example:
after_create_commit :foo1
after_create_commit :foo2
after_create_commit :foo3

here only foo3 gets executed and foo1, foo2 doesn't.

can someone explain this behaviour and what could be the proper solution for this?


Solution

  • Issue 1:

    after_create_commit :foo
    after_update_commit :foo
    

    The above code will not work as we cannot register same method in after_commit callback on both creation and updation. Here, the latter line of after_update_commit overrides the previous registration and callback will run after updation only.

    Reason:

    Rails does not maintain separate callback chains for create and update internally. We can check callback chain by using __callbacks[:commit] method.

    Also, Below code will work fine because these are procs and they will be registered as two separate procs in callback chain.

    after_create_commit -> { foo }
    after_update_commit -> { foo }
    

    Issue 2:

    after_create_commit :foo1
    after_create_commit :foo2
    after_create_commit :foo3
    

    Here, All methods should execute in the order foo3, foo2, foo1. But, If foo3 raises ActiveRecord::Rollback exception, then foo2 and foo1 will not execute. Please post the exact code that you are using.