怎么得到ActiveRecord :: Base.connection.execute的错误?

时间:2013-08-13 04:14:02

标签: 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 ::错误:22008(241)[unixODBC] [FreeTDS] [SQL Server]语法错误从字符串转换datetime。:INSERT INTO pinfo(birthdate)VALUES('1967-07-16 ?00:00:00)   执行(0.8ms)IF @@ TRANCOUNT> 0 ROLLBACK TRANSACTION

如何从ODBC::Error: 22008 (241) ...的{​​{1}}获得上述错误消息(ActiveRecord::Base.connection.execute)?

1 个答案:

答案 0 :(得分:2)

 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

由于