Created
May 16, 2014 01:52
-
-
Save jobwat/f4d0830c1a19bf7211ed to your computer and use it in GitHub Desktop.
SQL to CSV with Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sql='select event_type as code, country, state, location, name from tracks group by code, country, state, location, name order by code, country, state, location, name;' | |
pg_res = ActiveRecord::Base.connection.execute(sql) | |
csv_file = CSV.generate do |csv| | |
csv << %w{code country state location name} | |
pg_res.values.each { |line| csv << line } | |
end | |
File.write('tmp/tracks_export.csv', csv_file) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment