I'm able to import my .csv file from my server but now i simply want to delete it after the import occurs. I have all of the code in a rake task and I'm trying to delete the incidents.csv file after the task is completed. Here is my code.
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