Created
January 5, 2012 02:27
-
-
Save stevenharman/1563396 to your computer and use it in GitHub Desktop.
Dump a list database tables to CSV via ActiveRecord models.
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
| namespace :db do | |
| desc 'export the shit' | |
| task :export => [:environment] do | |
| require 'csv' | |
| ActiveRecordize('Brewery', 'Beer', 'Style', 'Category', 'Geocode') do |model| | |
| CSV.open("#{model.table_name}.csv", 'wb') do |csv| | |
| csv << model.column_names | |
| model.find_each { |m| csv << m.attributes.values } | |
| end | |
| end | |
| end | |
| def ActiveRecordize(*model_names) | |
| model_names.map do |klass| | |
| model = Kernel.const_set(klass, Class.new(ActiveRecord::Base)) | |
| yield model | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment