Skip to content

Instantly share code, notes, and snippets.

@lappi-lynx
Last active January 29, 2016 12:16
Show Gist options
  • Save lappi-lynx/1c408e5fd28f4462dd55 to your computer and use it in GitHub Desktop.
Save lappi-lynx/1c408e5fd28f4462dd55 to your computer and use it in GitHub Desktop.
Allows you to convert data from SQL into CSV format
# First connect to DB with needed sql data
class T < ActiveRecord::Base
establish_connection({adapter:'mysql2', database:'db_for_test', encoding:'utf8',user:'', password: ''})
set_table_name 'utility_company'
end
CSV.open("data.csv", "wb") do |csv|
csv << T.first.attribute_names # adds the attributes name on the first line
T.all.each do |hash|
csv << hash.attributes.values
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment