Skip to content

Instantly share code, notes, and snippets.

@MikaAK
Created June 19, 2014 20:15
Show Gist options
  • Save MikaAK/6f012b21d3d9c9fa86f5 to your computer and use it in GitHub Desktop.
Save MikaAK/6f012b21d3d9c9fa86f5 to your computer and use it in GitHub Desktop.
TeacherImporter
class TeachersImporter
def initialize(filename=File.dirname(__FILE__) + "/../db/data/students.csv")
@filename = filename
end
def import
field_names = ['name', 'email', 'address', 'phone_number']
print "Importing students from #{@filename}: "
Teacher.transaction do
File.open(@filename).each do |line|
data = line.chomp.split(',')
attribute_hash = Hash[field_names.zip(data)]
student = Teacher.create!(attribute_hash)
print "."; STDOUT.flush
end
end
puts "\nDONE"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment