7
  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]从字符串转换日期时间的语法错误。: INSERT INTO pinfo (birthdate) VALUES ('1967-07-16?00: 00:00') 执行 (0.8ms) 如果@@TRANCOUNT > 0 回滚事务

如何ODBC::Error: 22008 (241) ...ActiveRecord::Base.connection.executein 中获取上述错误消息 () rescue

4

1 回答 1

3
 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

谢谢

于 2013-08-13T08:08:42.317 回答