Created
August 13, 2010 16:57
-
-
Save unixmonkey/523195 to your computer and use it in GitHub Desktop.
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
| # execute by calling | |
| # rake db:import:from_csv | |
| # RAILS_ENV=production db:import:from_csv to use the production environment | |
| require 'ruport' | |
| namespace :db do | |
| namespace :import do | |
| desc 'import stuff into the database' | |
| task :from_csv => :environment do | |
| csv_file = Table(Rails.root.join('db','seeds','20100812_add_stuff.csv'), :records => true) | |
| csv_file.each do |row| | |
| record = Thing.new(row.to_hash) | |
| if record.valid? && record.save | |
| puts "Created Thing: #{record.attributes}" | |
| else | |
| puts "Skipping Thing: #{row.to_hash}" | |
| end | |
| end | |
| end | |
| desc 'export stuff from the database' | |
| task :to_csv => :environment do | |
| # the report_table method requires the Table | |
| # model have an acts_as_reportable declaration in it | |
| Thing.report_table( | |
| :all, | |
| :select => "field1, field2, field3", | |
| :order => "field3 ASC", | |
| :only => ([:field1, :field2]) | |
| ).save_as(Rails.root.join('db','export','export.csv')) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment