Loading data into Database reading from multiple files was never easy. I have been working on different projects lately, which required such exercise often. A general technique I followed to do that is explained here. In a gist, this is what it looks like:
require 'rubygems' require 'fastercsv' files.each do |key, value| file = "#{RAILS_ROOT}/db/drugsatfda/" + key recs = 0 puts "Working with #{value.pluralize}.." FasterCSV.foreach(file, :headers => true) do |row| begin obj = value.constantize.new(Array.to_hash(row.headers, row.fields)) obj.save recs += 1 rescue => e puts "Rows processed: " + recs.to_s puts e end end puts "Loaded #{recs} #{value.pluralize}" end
What do you think?