aasm / aasm/aasm

Oddness around rollbacks and after_commit callbacks

Open
#396 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
5.2k
Forks
644
PR merge metrics
No merged PRs in 30d

Description

During an aasm transition (before or after), I'm storing an array of values in a virtual attribute. If the commit of the instance is successful, I want to trigger an event using that array. If the commit fails (rollback), I don't want that event to trigger and I need to reset the array to nil.

I save instances in two ways using aasm:
customer.close!
and
customer.close
customer.save

By using aasm to save the instance (with the !), if there is a validation error (not having to do with the transition), the after_commit callback gets called no matter what and just says "false" and after_rollback doesn't get called.

On the other hand, if I do the second method of saving by using .save, the behavior is different and does trigger after_rollback.

Lastly, let's say the instance is invalid and I save it through aasm like so: customer.close!. It'll return false for the save but still trigger after_commit callback on rails, which seems really odd.

Am I doing something wrong or is this a bug?

I wrote a stackoverflow question about it as well: http://stackoverflow.com/questions/39381484/after-commit-not-working-as-expected

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the two entry points described: customer.close! versus customer.close followed by customer.save, with an invalid instance and the virtual array attribute. Trace the after_commit and after_rollback callbacks involved in each path. Done means the callback behavior and array reset are consistent with whether the save commits or rolls back.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.