2016-05-23 53 views
1

我想更新sql_for_insert,query,execute和update_sql为postgres生成的查询的所有sql语句。我正在使用rails 3.2.17。主要目的是将列名更改为小写。我试图在初始化器中定义一个类“class ActiveRecord :: ConnectionAdapters :: PostgreSQLAdapter”,在那里调用方法,但是我不能调用super,因为我的类没有从任何其他类扩展。activerecord PostgreSQLAdapter覆盖sql_for_insert,query,execute和update_sql

回答

1

我有一个备用解决方案,只更新一个位置的列名称。我创建了一个模块并将其包含在Arel:Attributes:Attribute中,该模块将列名转换为小写。此代码仍在测试中,尚未投入生产。但是这似乎解决了我的问题。

module ArelAttributeWrapper 
    def self.included base 
    @actualname = nil 
    base.class_eval do 
     def initialize(*args) 
     super(*args) 
     @actualname = args[1] 
     end 
     def name=(value) 
     @actualname = value.downcase 
     end 

     def name 
     @actualname.downcase 
     end 
    end 
    end 
end 

Arel::Attributes::Attribute.send(:include, ArelAttributeWrapper)