On Mar 20, 2012, at 8:49 PM, Tim Uckun wrote:
> I have done some googling to try and figure out how I can use COPY
> FROM STDIN with rails and have ran into some posts on stackoverflow or
> the mailing list but none of them seem to be working with rails 3.2
> Does anybody have a working example of using COPY FROM STDIN?
>
> Some things I have tried
>
> https://bitbucket.org/ged/ruby-pg/src/tip/sample/copyfrom.rb
> http://blog.edseek.com/archives/2009/04/26/putline-undefined-for-pgconn/
>
> and similar variations. Nothing seems to be working though.
I didn't do this on 3.2, but 3.0.x... not sure if this is one of the
stackoverflow options you tried or not...
conn = ActiveRecord::Base.connection_pool.checkout
raw = conn.raw_connection
raw.exec("COPY tablename (col1, col2, col3) FROM STDIN")
# open up your CSV file looping through line by line and getting the line into
a format suitable for pg's COPY...
rc.put_copy_data line
# once all done...
rc.put_copy_end
while res = rc.get_result do; end # very important to do this after a copy
ActiveRecord::Base.connection_pool.checkin(conn)
Source: http://stackoverflow.com/a/6780870/91830
-philip
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.