Ruby on Rails ActiveRecord Transactions Multiple database connections

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

A transaction acts on a single database connection. If you have multiple class-specific databases, the transaction will not protect interaction among them. One workaround is to begin a transaction on each class whose models you alter:

Student.transaction do
  Course.transaction do
    course.enroll(student)
    student.units += course.units
  end
end

This is a poor solution, but fully distributed transactions are beyond the scope of ActiveRecord.



Got any Ruby on Rails Question?