The ActiveRecord extension adds transaction support for operations using the ActiveRecord ORM. Like the other database extensions, it will roll back the transaction if any step returns a Failure.
Make sure you have activerecord installed:
Require and include the extension in your operation class:
include Dry::Operation::Extensions::ActiveRecord
transaction do
user = step create_user(input)
step create_profile(user)
user
end
end
# ...
end
By default, ActiveRecord::Base is used to initiate transactions. You can specify a different class either when including the extension:
include Dry::Operation::Extensions::ActiveRecord[User]
Or at runtime:
transaction(User) do
# ...
end
This is particularly useful when working with multiple databases in ActiveRecord.
You can also provide default transaction options when including the extension:
include Dry::Operation::Extensions::ActiveRecord[isolation: :serializable]
You can override these options at runtime:
transaction(isolation: :serializable) do
# ...
end
⚠️ Warning: The :requires_new option for nested transactions is not yet fully supported.