Skip to content

Improvements to ActiveRecord::Rollback docs #42874

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

Closed
wants to merge 4 commits into from
Closed

Conversation

jjb
Copy link
Contributor

@jjb jjb commented Jul 26, 2021

The main problem I wanted to solve is that the "Call tech support" message in the example is
confusing in this scenario, because it's addressing the user, but the user will never see it.
I'm not sure if I'm not understanding something, or maybe this was copy/pasted from
another example years ago.

Here I remove that and also expand the docs, hopefully illustrating the concept better.

Any and all feedback on copy, phrasing, formatting, examples, is welcome!

@p8
Copy link
Member

p8 commented Jul 26, 2021

Hey @jjb, thanks for the PR!

I can see how the "Call tech support" line is confusing.
The example is getting a lot complexer though now.

What do you think about adding the If this fails, user gets generic 500 behavior comment above the book.save!
and removing the "Call tech support" string like you did?

@jjb
Copy link
Contributor Author

jjb commented Jul 26, 2021

done - if that looks good i can squish the two commits

The example is getting a lot complexer though now

by this do you mean it's difficult to understand, or it's not emblematic of intended use case, or something else?

I think the docs currently in main are not emblematic of a typical use case, if you agree i wonder if we can improve the docs without making them too complex.

@p8
Copy link
Member

p8 commented Jul 26, 2021

@jjb adding the Author and Stats models makes things more complex.
Also, while the different responses ("rendering" and "redirecting") might be more realistic, they add more complexity that maybe isn't relevant to the example.
PRs are always welcome. If there is an another example you'd like to change we can discuss it another PR.

Copy link
Member

@p8 p8 left a comment

Choose a reason for hiding this comment

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

This looks good to me! 🚢
Please squash the commits.

the "Call tech support" message in the example is
confusing in this scenario, because it's addressing the user, but the user will never see it.

Also added a note emphasizing what behavior will be in the normal error case
@jjb
Copy link
Contributor Author

jjb commented Jul 26, 2021

@p8 squashed

Co-authored-by: Petrik de Heus <petrik@deheus.net>
jjb and others added 2 commits July 29, 2021 22:19
@jjb
Copy link
Contributor Author

jjb commented Jul 29, 2021

@p8 okay, done

would you consider replacing the today_is_friday? joke example for when the rollback is used? I feel it's not a good way to explain a possible real world use case and the jokiness creates a cognitive speed bump while trying to understand.

@p8
Copy link
Member

p8 commented Jul 29, 2021

I’m okay with that. I like some light hearted reading, but it shouldn’t be at the cost of understandability.

Maybe we should show the example in a model instead of a controller. An Order that requires the Book to be in the inventory? This allows us to skip all the rendering logic and use a more realistic example?

@jjb
Copy link
Contributor Author

jjb commented Jul 29, 2021

in that scenario, which operations should raise and which operations should continue on?

@rails-bot
Copy link

rails-bot bot commented Oct 27, 2021

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 Oct 27, 2021
@rails-bot rails-bot bot closed this Nov 3, 2021
@jjb
Copy link
Contributor Author

jjb commented Nov 23, 2021

Hi @p8 - if you have time and interest to answer my question I can maybe put something together, thanks!

@p8 p8 reopened this May 29, 2022
@rails-bot rails-bot bot removed the stale label May 29, 2022
@p8
Copy link
Member

p8 commented May 29, 2022

Hi @jjb sorry for the late reply 😞

Maybe we can render a different flash depending on whether the book was saved?
What do you think about something like:

  #   class BooksController < ActionController::Base
  #     def create
  #       book = Book.new(params[:book])
  #       Book.transaction do
  #         book.save!
  #         # If this fails, we rollback this transaction and pass on the exception.
  #         # The user gets generic 500 behavior.
  #         Stats.create!(type: 'book_purchase')
  #         if today_is_friday?
  #           # The system must fail on Friday so that our support department
  #           # won't be out of job. We silently rollback this transaction
  #           # without telling the user.
  #           raise ActiveRecord::Rollback
  #         end
  #       end
  #       # ActiveRecord::Rollback is the only exception that won't be passed on
  #       # by ActiveRecord::Base.transaction, so this will still be reached
  #       # even on Friday.
  #       if book.persisted?
  #         redirect_to root_url, notice: "Book purchased"
  #       else
  #         redirect_to root_url, alert: "Call tech support!"
  #       end
  #     end

@jjb
Copy link
Contributor Author

jjb commented Jun 19, 2022

thanks for looping back - i won't be able to work on this unfortunately - maybe someone else can pick it up and work with what i've suggested

@jjb jjb closed this Dec 5, 2024
@jjb jjb deleted the patch-1 branch December 5, 2024 22:29
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.

2 participants