Ruby-on-rails rails, activerecord, 获取当前连接规范

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8673193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:32:42  来源:igfitidea点击:

rails, activerecord, get current connection specification

ruby-on-railsactiverecord

提问by jrochkind

I am writing code that will migrate some data from one database to another, over-writing some data in the destination. It uses ActiveRecord, since it's associated with a Rails app using AR already.

我正在编写代码,将一些数据从一个数据库迁移到另一个数据库,覆盖目标中的一些数据。它使用 ActiveRecord,因为它已经与使用 AR 的 Rails 应用程序相关联。

Since some data will be over-written, I'd like to provide a confirmation prompt that tells the user the actual connection dictionary/spec used for the destination database connection. you know, adapter, host, username, password, database, the things you'd list in database.yml.

由于某些数据将被覆盖,我想提供一个确认提示,告诉用户用于目标数据库连接的实际连接字典/规范。你知道,适配器、主机、用户名、密码、数据库,你会在 database.yml 中列出的东西。

I can take the model for the stuff I'm writing to and ask for SomeModel.connection.... But there seems to be no API at all to get the actual connection spec out of a live connection object.

我可以采用我正在写的东西的模型并要求 SomeModel.connection .... 但似乎根本没有 API 可以从实时连接对象中获取实际的连接规范。

Really? Am I missing something? Any other ideas, even undocumented api?

真的吗?我错过了什么吗?任何其他想法,甚至未记录的api?

回答by Batkins

Similar to the way that you can call connectionon a model, you can make a call to connectionon ActiveRecord::Base.

与调用connection模型的方式类似,您可以调用connectionActiveRecord::Base。

ActiveRecord::Base.connection_config

Look at the docs for ActiveRecord::Base, as there are other methods that allow you to get/set attributes about the connection.

查看 ActiveRecord::Base 的文档,因为还有其他方法允许您获取/设置有关连接的属性。

回答by jrochkind

The answer is SomeModel.connection_config

答案是 SomeModel.connection_config