猴子补丁ActiveRecord的PostgreSQLAdapter方法

Pan*_*agi 1 activerecord monkeypatching ruby-on-rails ruby-on-rails-3

关于这个问题,我试图覆盖postgresql_version定义的方法ActiveRecord::ConnectionAdapters::PostgreSQLAdapter返回PostgreSQL版本:

module ActiveRecord
  module ConnectionAdapters
    class PostgreSQLAdapter < AbstractAdapter

      protected
        # Returns the version of the connected PostgreSQL server.
        def postgresql_version
          80200
        end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

但补丁未应用.我尝试在config/initializer中并在/ lib文件中要求它.救命?

Har*_*tty 7

试试这个:

# 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
Run Code Online (Sandbox Code Playgroud)

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