gpt4 book ai didi

ruby - to_sql 不适用于 update_attributes 或 .save

转载 作者:太空宇宙 更新时间:2023-11-03 17:37:14 24 4
gpt4 key购买 nike

我正在寻找一种方法来存储在更新或创建操作中生成的 sql 字符串。我试过将 .to_sql 附加到 update_attributes 的末尾,但它返回一个 TrueClass 错误(或类似的错误)。有什么我想念的吗?

最佳答案

In brief - you need to override ActiveRecord execute method. There you can add any logic for logging.

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
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

学分:

https://stackoverflow.com/a/1629474/643500

https://stackoverflow.com/a/1640560/643500

关于ruby - to_sql 不适用于 update_attributes 或 .save,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13037475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com