Skip to content

Conversation

@NikolasPilavakis
Copy link

Our FactoryBot linting started failing for a factory despite evidence that the trait combination was producing a valid model in test. We eventually traced that down to the change made in #1726. Introducing a ActiveRecord::Base.transaction meant that the after_commit hooks of our model weren't running during linting.

This is because of the way ActiveRecord::Base handles transactions. When changes are inserted for an ActiveRecord within an ActiveRecord::Base.transaction, that transaction keeps a list of all the records it contains changes for. as part of the transaction committing, *_commit record callbacks are fired

ActiveRecord::Base.transactions are joinable by default, which means that ActiveRecord can choose to join an existing transaction instead of inserting a new one. This means that *_commits that would previously result from the object's lifecycle are not sent until the end of the linting transaction.

To reinstate the previous behaviour, this PR ensures that the linting transaction is not joinable and any *_commit callbacks are respected.

Our FactoryBot linting started failing for a factory despite evidence
that the trait combination was producing a valid model in test. We
eventually traced that down to the change made in thoughtbot#1726. Introducing a
`ActiveRecord::Base.transaction` meant that the `after_commit` hooks of
our model weren't running during linting.

This is because of the way `ActiveRecord::Base` handles transactions.
When changes are inserted for an ActiveRecord within an
`ActiveRecord::Base.transaction`, that transaction keeps a list of all
the records it contains changes for. as part of the transaction
committing, `*_commit` record callbacks are fired

`ActiveRecord::Base.transaction`s are `joinable` by default, which means
that `ActiveRecord` can choose to join an existing transaction instead
of inserting a new one. This means that `*_commit`s that would
previously result from the object's lifecycle are not sent until the end
of the linting transaction.

To reinstate the previous behaviour, this PR ensures that the linting
transaction is not joinable and any `*_commit` callbacks are
respected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant