How to get error message out of ActiveRecord::Base.connection.raw_connection

I run this code to import a large csv file into a table in Postgres:

  connection = ActiveRecord::Base.connection.raw_connection
  connection.exec(%q[COPY import_xp_raw_bill_details FROM STDIN

data =
data.each_line {|line| connection.put_copy_data(line)}

In the code, after that in the same method I call the db via AR again to
confirm the records loaded (I have confirmed it can be any method, even
most simple AR find… lets say ‘User.all’), and I get the error below
that I have determined the error is due to the specific file I am
as the code works for other files… I would rather though get a real
message from Postgres if it is availalble rather than do trial and error
find the issue with the specific file).

(rdb:1) User.all
ActiveRecord::StatementInvalid Exception: PGError: ERROR: current
transaction is aborted, commands ignored until end of transaction block
SELECT “users”.* FROM “users”

From experience I know this means I made the god of PG unhappy and I am
allowed back for now as a punishment. However I can not seem to retrieve
specific error from the connection object above. I do see an
ActiveRecord::Base.connection.raw_connection#error_message method, but
returns an empty string.

Anyone have an idea where else to look… I have read the whole method
for this object (ActiveRecord::Base.connection.raw_connection) and tried
everything that looked promising.



This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs