我想更新为 sql_for_insert、query、execute 和 update_sql 生成的查询的所有 sql 语句。我正在使用 Rails 3.2.17。主要目的是将列名更改为小写。我试图在初始化程序中定义一个类“class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter”,在那里调用方法,但我不能调用 super,因为我的类不是从任何其他类扩展的。
最佳答案
我有另一种解决方案,只更新一个位置的列名。我制作了一个模块并将其包含在 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)
关于ruby-on-rails - activerecord PostgreSQLAdapter override sql_for_insert, query, execute 和 update_sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37389918/