Skip to content

Instantly share code, notes, and snippets.

@jobwat
Created May 16, 2014 01:52
Show Gist options
  • Save jobwat/f4d0830c1a19bf7211ed to your computer and use it in GitHub Desktop.
Save jobwat/f4d0830c1a19bf7211ed to your computer and use it in GitHub Desktop.
SQL to CSV with Rails
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