I'm trying to do a direct load to a PostgreSQL table in Rails. My connection is established like this:
rc = connection.raw_connection
rc.exec("COPY research FROM STDIN WITH CSV HEADER")
Here is a portion of the code:
for key,values in valuehash
entry = standard.model_constant::ATTRIBUTE_HASH[key.to_sym]
puts "Loading #{entry[:display]}..."
values.each do |value|
id = ActiveRecord::Base.connection.execute("select nextval('research_id_seq'::regclass)").first['nextval']
line = [id, value, entry[:display], standard.id, now, now]
until rc.put_copy_data( line.to_csv )
ErrorPrinter.print " waiting for connection to be writable..."
sleep 0.1
end
end
end
The line that begins id = fails. It says:
ActiveRecord::StatementInvalid: PG::UnableToSend: another command is already in progress
I don't want to write all these values to a CSV file, then read in that file. I just want to send the data from memory right to the database. I can't think of a valid reason why I couldn't run two connections to the same database against the same table, so I figured I must be doing something wrong. I've tried two raw connections, but it always keeps giving me the same connection from the pool.
rcconnection to get the id? Alternatively, why don't you create a model for the table so that you can use standard ActiveRecord to insert into the database? If you are worried about speed you can use github.com/zdennis/activerecord-import