Skip to content

Clarify that next can be used to exit transaction early#40052

Closed
zachahn wants to merge 1 commit intorails:masterfrom
zachahn:exit-transaction-with-next
Closed

Clarify that next can be used to exit transaction early#40052
zachahn wants to merge 1 commit intorails:masterfrom
zachahn:exit-transaction-with-next

Conversation

@zachahn
Copy link
Copy Markdown
Contributor

@zachahn zachahn commented Aug 14, 2020

Summary

This clarifies that next could be used to exit a transaction early. This is basically a documentation change!

In my testing, the next only exits out of the block, so completed gets set to true. I included that test, but I'm happy to remove it if it isn't helpful

Hope 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

@rails-bot
Copy link
Copy Markdown

rails-bot Bot commented Nov 13, 2020

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 your contributions.

@rails-bot rails-bot Bot added the stale label Nov 13, 2020
@rafaelfranca
Copy link
Copy Markdown
Member

Thank you for the pull request but I believe using next in this context is confusing and I prefer for us to not recommend it.

@matpowel
Copy link
Copy Markdown

matpowel commented Feb 9, 2021

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.

@rafaelfranca
Copy link
Copy Markdown
Member

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.

@matpowel
Copy link
Copy Markdown

matpowel commented Feb 9, 2021

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 :)

@wlipa
Copy link
Copy Markdown

wlipa commented Feb 14, 2021

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.

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.

4 participants