Created
June 19, 2014 20:15
-
-
Save MikaAK/6f012b21d3d9c9fa86f5 to your computer and use it in GitHub Desktop.
TeacherImporter
This file contains 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
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