猴子补丁ActiveRecord的PostgreSQLAdapter方法

时间:2013-02-23 18:03:18

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

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

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文件中要求它。帮助

1 个答案:

答案 0 :(得分: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

你的解决方案失败了,因为在修补猴子时你不能添加继承部分(即PostgreSQLAdapter < AbstractAdapter