You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Could you expand on this example? E.g. if a second table is modified within the user transaction, will that also get rolled back if there is an error with the user.create? what happens if there is an error with the other table operation, will that also fail the whole transaction?
For example, something like below, where we only want to create the user role if the user is successfully created:
user.transaction do
user.create(...)
user_role.create(...)
end
The text was updated successfully, but these errors were encountered:
Also, does the transaction have to be for a single table? in active record there was an ActiveRecord::Base.transaction that could be used instead of a specific table. Is there an equivalent concept here?
Transactions is a database concept here, transaction do is just an API for invoking BEGIN + COMMIT/ROLLBACK + a guarantee the same connection is used for all operations within the block. There's no way to distinguish between tables within a transaction in SQL. It works the same way in active-record, rom, sequel, whathaveyou. Yes, you can call User.transaction do or user_repo.transaction do but it'll end up with the same set of instructions for the database. The only caveat will be if there's more than one database in your app, only then should you worry.
I think, we could have a reference to a guide on transactions in general in the docs but I don't think we need to expand on the details of what transactions are there.
Could you expand on this example? E.g. if a second table is modified within the
user
transaction, will that also get rolled back if there is an error with theuser.create
? what happens if there is an error with the other table operation, will that also fail the whole transaction?For example, something like below, where we only want to create the user role if the user is successfully created:
The text was updated successfully, but these errors were encountered: