I would like to import the data from txt file to database in Ruby. I have tried to create a rake task to do so and struggling with finding a elegant way of doing it.
My Rake task so far:
desc "Import schools."
task :import_schools => :environment do
File.open(File.join(Rails.root, "imports", "schools.txt"), "r").each do |line|
if ! line.valid_encoding?
s = line.encode("UTF-16be", :invalid=>:replace, :replace=>"?").encode('UTF-8')
s.gsub(/dr/i,'med')
description, time, standards, books, choices = s.strip.split("\t")
u = ImportResult.new(:description => description, :time => time)
u.save
end
end
end
My txt file datas like:
primary 23484775884 standard:fifth book:science choice:maths name:Joseph city:London
secondary 46537728836 standard:fourth book:english choice:maths name:Jain city:Manchester
.........
I want to insert each of these record into ImportResult database and ignore name and city of each record.
Expected Result
ImportResult Table:
id: 1
description: primary
time: 23484775884
standard: fifth
choice_of_book: science
Thanks
Aucun commentaire:
Enregistrer un commentaire