rails,activerecord,获取当前连接规范 [英] rails, activerecord, get current connection specification

查看:70
本文介绍了rails,activerecord,获取当前连接规范的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

我可以将要写的东西作为模型,并请求SomeModel.connection...。但是似乎根本没有API可以得到实时连接对象中的实际连接规格。

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.

真的吗?我想念什么吗?还有其他想法,甚至没有公开的api?

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

推荐答案

类似于您调用连接的方式在模型上,您可以在ActiveRecord :: Base上调用连接

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

ActiveRecord::Base.connection_config

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

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

这篇关于rails,activerecord,获取当前连接规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆