Skip to content

Instantly share code, notes, and snippets.

@stevenharman
Created January 5, 2012 02:27
Show Gist options
  • Select an option

  • Save stevenharman/1563396 to your computer and use it in GitHub Desktop.

Select an option

Save stevenharman/1563396 to your computer and use it in GitHub Desktop.
Dump a list database tables to CSV via ActiveRecord models.
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