什么时候ActiveRecord的建立连接? [英] When does ActiveRecord establish connections?

查看:126
本文介绍了什么时候ActiveRecord的建立连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一些Rails应用程序,我的ActiveRecord模型似乎建立在初始化数据库连接(例如,当我这样做轨控制台),而在其他国家连接似乎是只有建立在我引用的模型类或实例化一个模型对象。

例如,我刚刚去了一个应用程序,打开了Rails的控制台,并写道:

  SomeModel.connected?
 

和它返回。我去到另一个应用程序,输入了相同的命令(不同的模型),它返回。我去了第三次申请,进入了相同的命令。这一次,等了一会儿,然后回到,这让我觉得连接?方法触发的连接出于某种原因。

在这种行为上的差异似乎并不相关的Rails版本或型号的内容。这可能是一个很奇怪的,我在我的初始化工作要做,但我不这么认为。

所以,当它的Rails建立连接?或者有什么是预期的行为?

其他信息

我要补充一点,它似乎并不像连接?返回false,由于Rails的不能连接到数据库。

例如,在我的第一个应用程序,我做的:

  SomeModel.connected?
#=>假
SomeModel.table_exists? #或让Rails的看着分贝任何其他命令
#=>真正
SomeModel.connected?
#=>真正
 

解决方案

回答我的问题:

无论是数据库连接在Rails的初始化过程实际上初始化基本上就是否的ActiveRecord :: Base.connection来(而不是 establish_connection )在初始化过程中被调用。

这可能与Rails的版本:例如,在Rails的3.2.13中,active_record.validate_explain_support初始化会调用连接

 !的ActiveRecord :: Base.connection.supports_explain?
 

在Rails的3.2.14,此调用不言。

不过,Rails的3.2.14可以打电话到在active_record.set_reloader_hooks初始化连接。这个调用可以用命令发生

 的ActiveRecord :: Base.clear_cache!
 

虽然 prepare 回调亚军并不似乎总是调用此...

我还发现,一些宝石(例如, ActiveAdmin )有一个初始化过程,将调用连接在一些点。

In some of my Rails applications, my ActiveRecord models seem to establish db connections on initialization (e.g., when I do rails console), while in others connections seem to be established only when I reference the model class or instantiate a model object.

For example, I just went to one application, opened Rails console and wrote:

SomeModel.connected?

and it returned false. I went to another application, entered the same command (for a different model), and it returned true. I went to a third application and entered the same command. This time, it waited a moment and then returned true, which made me think that the connected? method triggered the connection for some reason.

This difference in behavior doesn't seem related to Rails versions or the contents of the models. It could be something weird I've done in my initializers, but I don't think so.

So when does Rails establish connections? Or what is the expected behavior?

ADDITIONAL INFO

I'll add that it doesn't seem like connected? returns false because Rails cannot connect to the database.

For example, in my first application I do:

SomeModel.connected?
# => false
SomeModel.table_exists? # or any other command that makes Rails look at db
# => true
SomeModel.connected?
# => true

解决方案

Answering my own question:

Whether a database connection is actually initialized during the Rails initialization process depends basically on whether ActiveRecord::Base.connection (not establish_connection) is called during the initialization process.

This can be related to the Rails version: for example, in Rails 3.2.13, the "active_record.validate_explain_support" initializer makes a call to connection:

!ActiveRecord::Base.connection.supports_explain?

In Rails 3.2.14, this call is not made.

However, Rails 3.2.14 may make a call to connection in the "active_record.set_reloader_hooks" initializer. This call may occur with the command

ActiveRecord::Base.clear_cache!

although the prepare callback runner doesn't always seem to call this...

I also found that some gems (e.g., ActiveAdmin) have an initialization process that will call connection at some point.

这篇关于什么时候ActiveRecord的建立连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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