The answer is really remove_connection( klass=self) . However, establish_connection(...) returns a connection, not a base class, so the code should be:
ActiveRecord::Base.establish_connection(...) ActiveRecord::Base.remove_connection( ActiveRecord::Base)
To distinguish between different connections (for example, useful for processing multiple databases), you can create a subclass to simplify it. This will disconnect the connected connection, and even with repeated calls that do not belong to the parent class.
For instance:
class MyDatabase::Base < ActiveRecord::Base def example_connection_and_disconnection MyDatabase::Base.establish_connection(...) MyDatabase::Base.remove_connection( MyDatabase::Base) end end
Hope this helps others. :-)
source share