RoR ActiveRecord -- What happens behind the scenes if you try to do x.save if x came from a read replica

22 Views Asked by At

I just migrated to rails 6, and am testing the new multi db functionality. I have a remote read db running in aurora, and a write database locally to simulate a read and write cluster.

I am pulling a record from the read replica, and trying to save it locally:

x = nil
ActiveRecord::Base.connected_to(role: :reading) do
    x = Integration.last
end

x.save

When I run x.save, it goes through all the validations and returns true, but I don't see it in my write db.

It does, however, work if I something like:

i = x.dup
i.save

though the sequence does of course change.

I know that http requests should be routed correctly in this case, but how would you handle it if you wanted to call a database manually? Thinking of moving some of our jobs over to use read replicas as well. Does anyone know best practices in this case?

0

There are 0 best solutions below