【问题标题】:Monkey patch ActiveRecord's PostgreSQLAdapter methodMonkey 补丁 ActiveRecord 的 PostgreSQLAdapter 方法
【发布时间】:2013-02-23 18:03:18
【问题描述】:

关于this question,我正在尝试覆盖ActiveRecord::ConnectionAdapters::PostgreSQLAdapter 中定义的返回PostgreSQL 版本的postgresql_version 方法:

module ActiveRecord
  module ConnectionAdapters
    class PostgreSQLAdapter < AbstractAdapter

      protected
        # Returns the version of the connected PostgreSQL server.
        def postgresql_version
          80200
        end
    end
  end
end

但是补丁没有应用。我在 config/initializer 中尝试过,并在 /lib 文件中要求它。帮忙?

【问题讨论】:

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


    【解决方案1】:

    试试这个:

    # ensure ActiveRecord's version has been required already
    require 'active_record/connection_adapters/postgresql_adapter'
    
    class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter 
    protected
      def postgresql_version
        80200
      end
    end
    

    您的解决方案失败了,因为您无法在猴子修补时添加继承部分(即PostgreSQLAdapter &lt; AbstractAdapter

    【讨论】:

    • 试过了,但我得到了:TypeError: superclass mismatch for class PostgreSQLAdapter
    • 好的,需要 postgresql_adapter 来解决它:require 'active_record/connection_adapters/postgresql_adapter'
    猜你喜欢
    • 2017-05-24
    • 2011-12-15
    • 1970-01-01
    • 2012-08-03
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    相关资源
    最近更新 更多