1 class ConnectionSwitcher 2 def initialize 3 self.handlers = {readonly: ConnectionHandler.new} 4 end 5 6 def with_readonly(&block) 7 before = ActiveRecord::Base.connection_handler 8 ActiveRecord::Base.connection_handler = handlers[:readonly] 9 yield 10 ensure 11 ActiveRecord::Base.connection_handler = before 12 end 13 end