Skip to content

Instantly share code, notes, and snippets.

@ashaw
Created June 19, 2012 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashaw/eb9769bcfc86ca0663da to your computer and use it in GitHub Desktop.
Save ashaw/eb9769bcfc86ca0663da to your computer and use it in GitHub Desktop.
task :import => :environment do
Organization.delete_all
f = FasterCSV.open("#{Rails.root.to_s}/db/initial/data.csv", :headers => true)
f.each do |row|
record = Organization.new
org = row.to_hash
org.each do |field, val|
next unless val
val = val.strip
if Organization.columns_hash[field].type == :integer
val = val.gsub(/[,\$]/,"").to_f
end
if Organization.columns_hash[field].type == :boolean
val = val.downcase =~ /(yes|x+)/ ? true : false
end
record[field] = val
end
p record
record.save
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment