I have an application with two databases, a global one (which has a primary and replica) and a local one (which also has a primary and replica). I would like to read from the global database (because the primary is located far away) but write to the local database, e.g.:
# Setup
module Global
class ApplicationRecord < ::ApplicationRecord
self.abstract_class = true
connects_to database: {writing: :global_leader, reading: :global_follower}
end
end
module Local
class ApplicationRecord < ::ApplicationRecord
self.abstract_class = true
connects_to database: {writing: :local_leader, reading: :local_follower}
end
end
module Local
class User < ApplicationRecord
end
end
# Code I would like to do but it raises ActiveRecord::ReadOnlyError
Global::ApplicationRecord.connected_to(role: :reading) do
Local::User.last.touch
end
At the moment rails/connection_handling.rb at e00f9c8aaea3fbedc2d19926da4f4575c12f0aaa · rails/rails · GitHub makes that impossible, is there any plan to allow roles per model?