如何断开与rails中的特定数据库的连接?

And*_*hyn 7 activerecord ruby-on-rails

我使用此代码段连接到另一个数据库

ActiveRecord::Base.establish_connection....
Run Code Online (Sandbox Code Playgroud)

但我不知道如何在不需要之后删除此连接.

dee*_*our 9

你可以打电话 remove_connection

old_connection = ActiveRecord::Base.remove_connection
Run Code Online (Sandbox Code Playgroud)

如果你做了类似下面的事情(有任务)

new_connection = ActiveRecord::Base.establish_connection(...)
Run Code Online (Sandbox Code Playgroud)

这可以传递给 remove_connection

old_connection = ActiveRecord::Base.remove_connection(new_connection)
Run Code Online (Sandbox Code Playgroud)

您可以在源代码中找到它.