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

DELIMITERS ‘,’ CSV])
data = File.open(file_path).read
data.each_line {|line| connection.put_copy_data(line)}
connection.put_copy_end

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
the
most simple AR find… lets say ‘User.all’), and I get the error below
(note
that I have determined the error is due to the specific file I am
importing,
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
to
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
not
allowed back for now as a punishment. However I can not seem to retrieve
a
specific error from the connection object above. I do see an
ActiveRecord::Base.connection.raw_connection#error_message method, but
it
returns an empty string.

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

Thanks,

David

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