2013-02-23 174 views
1

關於this question,我試圖覆蓋ActiveRecord::ConnectionAdapters::PostgreSQLAdapter定義的postgresql_version方法返回PostgreSQL的版本:猴子補丁的ActiveRecord的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 

但不應用補丁。我嘗試在一個配置/初始化程序,並要求它在一個/ lib文件。幫幫我?

回答

6

試試這個:

# 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

+0

嘗試這樣做,你不能添加繼承部分,但我得到:'類型錯誤:類超不匹配PostgreSQLAdapter' – 2013-02-25 20:19:48

+0

確定需要postgresql_adapter解決它:'require'active_record/connection_adapters/postgresql_adapter'' – 2013-02-25 20:30:21