Rails 应用程序中的猴子修补数据库适配器

syn*_*pse 1 ruby ruby-on-rails ruby-on-rails-3

我正在使用 PosgreSQL 适配器连接到 Vertica 数据库,该数据库主要与 PostgreSQL 兼容,但不支持像 client_min_messages 这样的选项(PGconn.connect尽管不存在于 中,但仍会传递到database.yml)。我已经制作了一个快速而肮脏的猴子补丁,ActiveRecord::ConnectionAdapters::PostgreSQLAdapter但问题是我猜 AR 中的所有内容都是延迟加载的,并且在我的补丁之后读取原始文件。

如果我require 'active_record/connection_adapters/postgresql_adapter'在猴子补丁的顶部添加,则 ActiveRecord 尝试建立连接并失败。是否可以改变这种行为以使猴子补丁发挥作用,或者我应该编写一个成熟的连接适配器?

Sha*_*ler 5

您可以将您的代码挂接至railties 初始化。包括来自我的 gem multi_config 的示例:

module <YourModule>
  # Railtie subclass for the gem/plugin
  class Railtie < Rails::Railtie

    # Railtie initializer method
    initializer '<your_plugin>.active_record' do

      # When active_record is loaded, only then run this.
      ActiveSupport.on_load :active_record do
        # Hook your code here. For .e.g. 
        ActiveRecord::Base.send(:include, <YourPluginModule>)
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)