【发布时间】:2014-10-15 16:39:59
【问题描述】:
我可以从我的服务器导入我的 .csv 文件,但现在我只想在导入完成后将其删除。我在 rake 任务中有所有代码,我正在尝试在任务完成后删除 events.csv 文件。这是我的代码。
require 'csv'
require 'open-uri'
namespace :import_incidents_csv do
task :create_incidents => :environment do
puts "Import Incidents"
#csv_text = File.read('/Users/Ben/Sites/ror/LFD/incidents.csv', :encoding => 'windows-1251:utf-8')
csv_text = open("http://www.##########.com/###########/incidents.csv") {|f| f.read}
csv = CSV.parse(csv_text, :headers => true)
@incident_id_array = []
@report_nr_array = []
csv.each do |row|
row = row.to_hash.with_indifferent_access
Incident.create!(row.to_hash.symbolize_keys)
@incident_id_array << Incident.last.id
@report_nr_array << Incident.last.report_nr
end
#------This combines the incidents array and the report_nr array into a hash
@report_incident_hash = {}
@report_nr_array.each_with_index do |value, index|
@report_incident_hash[value] = @incident_id_array[index]
end
#puts @report_incident_hash
#----------------------------------------------------------------------------
end
end
【问题讨论】:
标签: ruby-on-rails ruby csv rake