简体   繁体   中英

Rails 5.2 multiple database connections issue

We have found a few patterns for creating a Rails app with multiple database connections (For example: https://www.thegreatcodeadventure.com/managing-multiple-databases-in-a-single-rails-application/

The pattern we use is as follows (Actual example):

# models/icm_db_base.rb
class IcmDbBase < ActiveRecord::Base
  self.abstract_class = true
  establish_connection :icm_production
end

Then, for a model that I want to implement this connection, I use:

# models/icm_task.rb
class IcmTask < IcmDbBase
... model code here
end

While this does all load up, we get the following oddity when I try to run a test inside rails console (This behavior is replicable on dev and prod):

Loading development environment (Rails 5.2.3)
2.6.4 :001 > IcmTask.connection_config
=> DATABASE CONNECTION CONFIG FOR ActiveRecord::Base (DB #1)

2.6.4 :002 > IcmTask.superclass
 => IcmDbBase(abstract) 

2.6.4 :003 > IcmDbBase.connection_config
 => DATABASE CONNECTION CONFIG FOR IcmDbBase (DB #2)

It becomes clear that this output (While not expected) is actually what has happened, and IcmTask is inheriting its connection from ActiveRecord::Base and NOT inheriting from IcmDbBase as desired/expected.

We are running Rails 5.2.3 and Ruby 2.6.4.

Even more strange is that if I put a log into our web server (puma 4.3.0) the connection config for IcmTask DOES match IcmDbBase as expected and everything works as desired.

Anything obvious here? We cannot get this to inherit the connection as expected for this model no matter what we've tried while inside rails console.

原来 innkeeper gem 开箱即用地覆盖了此连接继承行为(只需安装它),因此当我删除 gem 时,它的行为符合预期。

Try

IcmTask.connection_db_config

instead of

IcmTask.connection_config

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM