【问题标题】:Monkey-patching database adapter in Rails appRails 应用程序中的猴子修补数据库适配器
【发布时间】:2012-10-27 11:06:16
【问题描述】:

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

如果我在猴子补丁的顶部添加require 'active_record/connection_adapters/postgresql_adapter',那么 ActiveRecord 会尝试建立连接并失败。是否可以更改此行为以使猴子补丁正常工作,还是我应该只编写一个成熟的连接适配器?

【问题讨论】:

  • 你有完整的模块吗?
  • 嗨@synapse,在将 postgres 升级到 12 版本后,Rails3 面临同样的问题。请问你是怎么解决这个问题的?

标签: ruby-on-rails ruby ruby-on-rails-3


【解决方案1】:

您可以将代码挂接到 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

【讨论】:

    猜你喜欢
    • 2021-03-10
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    • 2012-03-13
    • 2013-12-26
    • 2012-06-14
    • 2012-03-29
    • 1970-01-01
    相关资源
    最近更新 更多