When I have 2 objects to save inside a transaction
a = A.new(...)
b = B.new(...)
Does it matter on which model class I invoke the transaction method?
A.transaction do
a.save
b.save
end
or
B.transaction do
a.save
b.save
end
IMNO both use the same db transaction, because ActiveRecord can only handle one connection, thus it should not matter. Is that correct?
Thanks, Alex.
Yes, you are correct provided both classes use the same database connection. It is possible for a class to use establish_connection
to connect to a different database but you would know if you were doing that. So, as you correctly suggest using either A.transaction
or B.transaction
is fine.
If they were using different databases you could nest the transaction calls:
A.transaction do
B.transaction do
...
end
end
but that is not necessary in this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With