ruby-on-rails - 如何获取 ActiveRecord::Base.connection.execute 的错误?

标签 ruby-on-rails activerecord

  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/

相关文章:

ruby-on-rails - 如何在 Ruby on Rails/ActiveRecord 中克隆模型?

php - 带有不太正常语句的 Codeigniter Active Record Select 语句

ruby-on-rails - Rails 获取 "each"循环的索引

ruby-on-rails - 如何使用 Rails 和 Active Record 在单个列中找到紧密匹配项?

ruby-on-rails - Heroku 上的 ffmpeg : unrecognized option 'preset'

ruby-on-rails - 使用 ActiveResource 在 Rails 中使用非 REST API

ruby-on-rails - Rails 克隆记录

ruby-on-rails - 按不同顺序条件合并两个数组

ruby - 覆盖 ActiveRecord::QueryMethods 中的方法

ruby-on-rails - Rails ActiveRecord - update_all : how to update multiple fields at once with mixed type of arguments