ActiveRecord的3.1.0多个数据库 [英] ActiveRecord 3.1.0 multiple databases
问题描述
我想升级的ActiveRecord宝石最新的3.1.0版本,看到被提出了很多异常,我认为这是由于我们如何处理多个数据库。
I'm trying to upgrade the ActiveRecord gem to the latest 3.1.0 release and seeing a lot of exceptions being raised, I think it's due to how we handle multiple databases.
有关我们的每一个数据库,我们指定继承的ActiveRecord :: Base的
一个单独的基类,并调用 establish_connection
在那里。有没有跨数据库的关系。这工作得很好我们到现在为止。
For each of our databases we specify a separate base class which inherits from ActiveRecord::Base
, and call establish_connection
in there. There are no cross-database relations. This has worked fine for us up until now.
已经升级到3.1.0的ActiveRecord我看到它未能与的ActiveRecord :: ConnectionNotEstablished
例外,遍历关系的时候(例如,它会成功拉一个单一的实体或导航到一个相关的类时)设置它们的从DB,但失败。
Having upgraded to ActiveRecord 3.1.0 I'm seeing that it fails with an ActiveRecord::ConnectionNotEstablished
exception, when traversing relations (i.e. it will successfully pull a single entity or set of them from the DB, but fails when navigating to a related class).
回溯的最上面一行是<$c$c>C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:410:in retrieve_connection,所以我挖成这一点。该方法被定义如下:
The top line of the backtrace is C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:410:in 'retrieve_connection'
, so I dug into this a little. The method is defined as follows:
def retrieve_connection(klass) #:nodoc:
pool = retrieve_connection_pool(klass)
(pool && pool.connection) or raise ConnectionNotEstablished
end
我的简单测试(把Customer.first.address
)调用 retrieve_connection
3倍。两次客户
为克拉斯
参数,并曾经与的ActiveRecord :: Base的
作为参数 - 这是当它失败的 establish_connection
尚未要求的ActiveRecord :: Base的
My simple test (puts Customer.first.address
) calls retrieve_connection
3 times. Twice with Customer
as the klass
parameter, and once with ActiveRecord::Base
as the parameter - which is when it fails as establish_connection
has not been called for ActiveRecord::Base
.
到实际问题的话 - 有没有处理的ActiveRecord的多个数据库连接了新推荐的方式?如果是这样,那是什么?
To the actual question then - is there a new recommended way of handling multiple database connections in ActiveRecord? If so, what is it?
如果不是,这可能是造成这个问题?
If not, what could be causing this problem?
推荐答案
昨天我碰到了同样的问题,而升级到ActiveRecord的3.1.0。我无法是否有处理的ActiveRecord的3.1多个数据库连接了新推荐的方式说话,但我没有找到一个方法来解除自己。
I ran into the same issue yesterday while upgrading to ActiveRecord 3.1.0. I can't speak to whether there is a new recommended way of handling multiple database connections in ActiveRecord 3.1, but I did find a way to unblock myself.
它会出现一个连接现在必须建立在的ActiveRecord :: Base的,以便它确定适配器的表名长度/规则。随着我建立了我的数据库初始化连接的休息,我现在也有一个ActiveRecord :: Base的连接建立我的数据块一(它并不重要哪一个)。
It appears a connection must now be established on ActiveRecord::Base in order for it to determine the table name lengths/rules of the adapter. Along with the rest of my connections established in my database initializer, I now also have an ActiveRecord::Base connection established to one of my DBs (it doesn't matter which one).
我想,认为有一个更好的解决方案被发现,但我很高兴能畅通现在。
I'd like to think there's a better solution to be found, but I'm happy to be unblocked for now.
这篇关于ActiveRecord的3.1.0多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!