begin
ActiveRecord::Base.transaction do
// ...
sanitized_sql = "INSERT INTO pinfo ..."
ActiveRecord::Base.connection.execute(sanitized_sql)
end
rescue
// how can I get the error?
end
在 webrick 控制台中,错误 (
1967-07-16?00:00:00
) 显示为:EXECUTE (0.0ms) ODBC::Error: 22008 (241) [unixODBC][FreeTDS][SQL Server]Syntax error converting datetime from character string.: INSERT INTO pinfo (birthdate) VALUES ('1967-07-16?00:00:00') EXECUTE (0.8ms) IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION
如何获得从
ODBC::Error: 22008 (241) ...
引发的上述错误消息( ActiveRecord::Base.connection.execute
)在 rescue
?
最佳答案
begin
ActiveRecord::Base.transaction do
// ...
sanitized_sql = "INSERT INTO pinfo ..."
ActiveRecord::Base.connection.execute(sanitized_sql)
end
rescue Exception => exc
logger.error("Message for the log file #{exc.message}")
flash[:notice] = "Store error message #{exec.message}"
end
谢谢
关于ruby-on-rails - 如何获取 ActiveRecord::Base.connection.execute 的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18200558/