Clarify that next can be used to exit transaction early#40052
Clarify that next can be used to exit transaction early#40052zachahn wants to merge 1 commit intorails:masterfrom
next can be used to exit transaction early#40052Conversation
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
|
Thank you for the pull request but I believe using |
|
I want to register a vote to re-open this. It was a massive relief to me to hear that "next" will work in a logical manner. I find it totally expected that next should exit the transaction block successfully, what is confusing? In a complex block, this style feels orders of magnitude better than having deeply nested if/then/else blocks which is a nightmare. |
|
Thank you for voting, but I still prefer to not recommend it. You can use if you want, but the framework will not recommend a key word that says "next" when there is no next iteration to run. |
|
I'm sure you're aware of this, but "next" doesn't actually run the "next iteration" but rather stops execution of the current block and returns control to the block. In the context of an iterating block, obviously that will result in evaluating whether to run the next iteration but that is not a given. That's why it works in this case so well. It's up to you and the Rails team of course but we have found the deprecation and copious warnings frustrating because the required fixes are just bad code (Rubocop hates it). All of those frustrations would have been zero with this documentation change :) |
|
This tip helped me a lot. I don't actually have any idea how I would have changed my code to work without a return / next because there were two nested blocks involved. It certainly would have been an uglier change. So +1 vote for the documentation change from me. |
Summary
This clarifies that
nextcould be used to exit a transaction early. This is basically a documentation change!In my testing, the
nextonly exits out of the block, socompletedgets set to true. I included that test, but I'm happy to remove it if it isn't helpfulHope this helps!
Other Information
I recently wrote a transaction block that always marked the record as completed, but only conditionally wrote some more data after that write. So the fix in #39453 wouldn't have affected this case