作为这篇文章的后续:我正在尝试记录所有正在生成的查询。我已经尝试将它放在初始化程序中,并且直接在控制台中运行它并执行似乎永远不会被执行。
我尝试了以下方法:
connection = ActiveRecord::Base.connection
class << connection
alias :original_exec :execute
def execute(sql, *name)
# try to log sql command but ignore any errors that occur in this block
# we log before executing, in case the execution raises an error
raise "THROW AN ERROR"
begin
file = File.open(RAILS_ROOT + "/log/sql.txt",'a'){|f| f.puts Time.now.to_s+": "+sql}
rescue Exception => e
;
end
# execute original statement
original_exec(sql, *name)
end
end
然后运行
Person.last
而且我仍然没有错误。