Skip to content

Instantly share code, notes, and snippets.

@jcasimir
Created February 26, 2012 17:39
Show Gist options
  • Save jcasimir/1917900 to your computer and use it in GitHub Desktop.
Save jcasimir/1917900 to your computer and use it in GitHub Desktop.
Export ActiveRecord Tables to CSV
require 'csv'
class Exporter
DEFAULT_EXPORT_TABLES = [ Invoice, InvoiceItem, Item, Merchant, Transaction, User ]
DESTINATION_FOLDER = "tmp/"
def self.export_tables_to_csv(tables = DEFAULT_EXPORT_TABLES)
tables.each { |klass| export_table_to_csv(klass) }
end
def self.export_table_to_csv(klass)
output_file = CSV.open(filename_for_class(klass), "w")
output_file << klass.headers
klass.data.each{ |row| output_file << row }
output_file.close
end
def self.filename_for_class(klass)
DESTINATION_FOLDER + klass.to_s.underscore + '.csv'
end
end
class ActiveRecord::Base
def self.data
find(:all).map{ |i| i.data}
end
def self.headers
columns.map(&:name)
end
def data
self.class.headers.map { |column| self.send(column) }
end
end
@pjb3
Copy link

pjb3 commented Feb 26, 2012

SELECT INTO OUTFILE BRO

@ccalvert
Copy link

I note that two of the three comments assume that the database is MySQL.

@norman
Copy link

norman commented Feb 27, 2012

@ccalvert my comment includes the command for Postgres, so no, I didn't assume that.

@ccalvert
Copy link

@norman, wow, I read your code too fast, didn't I? Sorry about that. Comment retracted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment