Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Callbacks Documentation: Specify 'after_commit' and 'after_rollback' Behavior for Multiple Objects of the Same Record [ci skip] #49831

Conversation

dhavalsingh
Copy link

… multiple instances of the same model

Motivation / Background

This Pull Request has been created because this is a pretty nuanced case that can come up in complex applications. We had a bug due to this and I couldn't find any other source specifying this behavior. The rails guides should be clear on this case so folks can avoid this and be mindful of this behavior when designing or maintaining systems.

Detail

This Pull Request changes the AR Callback Docs.

Additional information

#39400 This is the only place that references this.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@rails-bot rails-bot bot added the docs label Oct 28, 2023
@dhavalsingh dhavalsingh changed the title Update Callbacks Documentation: Clarify commit callback behavior with… Update Callbacks Documentation: Clarify commit callback behavior with… [ci skip] Oct 28, 2023
@dhavalsingh dhavalsingh changed the title Update Callbacks Documentation: Clarify commit callback behavior with… [ci skip] Update Callbacks Documentation: Clarify commit callback behavior with multiple instances of the same model [ci skip] Oct 28, 2023
@dhavalsingh
Copy link
Author

Can someone check why the buildkite check is failing? or re-run it. I didn't put the [ci skip] tag at first and then edited it.
Should i raise another PR?

@@ -554,6 +554,8 @@ WARNING. The code executed within `after_commit` or `after_rollback` callbacks i

WARNING. Using both `after_create_commit` and `after_update_commit` with the same method name will only allow the last callback defined to take effect, as they both internally alias to `after_commit` which overrides previously defined callbacks with the same method name.

WARNING. In the context of a single transaction, should you interact with multiple instances of the same model, `after_commit` or `after_rollback` callback behavior is designed to execute callbacks for only the first instance of the model. Subsequent instances of the same model within that transaction will _not_ trigger their own `after_commit` or `after_rollback` callback. This is a nuanced behavior that can impact the predictability and flow of callback execution, particularly in scenarios where multiple instances of the same model are undergoing changes in a single transaction.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide a simple example of what this would look like or how you're doing this transaction?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#39400
It's given in this PR.
Or. did you mean i should add an example to the docs?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking an actual code example but since it's mentioned in that PR never mind.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated the msg to be more clear. I think multiple instances might be confusing

@dhavalsingh dhavalsingh changed the title Update Callbacks Documentation: Clarify commit callback behavior with multiple instances of the same model [ci skip] Update Callbacks Documentation: Specify 'after_commit' and 'after_rollback' Behavior for Multiple Objects of the Same Record [ci skip] Oct 29, 2023
@dhavalsingh
Copy link
Author

Can anyone review this? I really think this should be added in the guides as there is little to no chance the behaviour will change in the future as old apps can break.

@dhavalsingh
Copy link
Author

Needs ActiveRecord tag @zzak , also feedback one. Thanks

Copy link
Contributor

@adrianna-chang-shopify adrianna-chang-shopify left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! The doc changes look good to me. Can you wrap your lines at a column width of 80 characters and squash your commits please?

@dhavalsingh dhavalsingh force-pushed the update_callback_docs_for_same_model_multi_instance branch from cde889e to e5ba8d9 Compare November 1, 2023 08:28
… multiple instances of the same model

updated warnign to be more clear

Update Callbacks Documentation: Clarify commit callback behavior
@dhavalsingh dhavalsingh force-pushed the update_callback_docs_for_same_model_multi_instance branch from e5ba8d9 to 2e6f1dc Compare November 1, 2023 08:56
@dhavalsingh
Copy link
Author

@adrianna-chang-shopify Done! Thanks for the review.

@adrianna-chang-shopify adrianna-chang-shopify merged commit 1370d04 into rails:main Nov 1, 2023
3 checks passed
adrianna-chang-shopify added a commit that referenced this pull request Nov 1, 2023
…ame_model_multi_instance

Update Callbacks Documentation: Specify 'after_commit' and 'after_rollback' Behavior for Multiple Objects of the Same Record [ci skip]
@p8
Copy link
Member

p8 commented Nov 1, 2023

I noticed the examples underneath the warnings related to the third warning, instead of this new warning.
So I've moved this warning beneath the examples: #49884

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants