Loading data from multiple files in Ruby using Hash

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|
			obj = value.constantize.new(Array.to_hash(row.headers, row.fields))

			recs += 1
		rescue => e
			puts "Rows processed: " + recs.to_s
			puts e
	puts "Loaded #{recs} #{value.pluralize}"

What do you think?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.