从服务器导入后删除.csv文件

时间:2014-10-15 16:40:00

标签: ruby-on-rails ruby csv rake

我能够从我的服务器导入我的.csv文件,但现在我只想在导入后删除它。我在rake任务中拥有所有代码,并且在完成任务后我试图删除incidents.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

1 个答案:

答案 0 :(得分:0)

如果您可以ssh到服务器,请尝试net/ssh gem并运行'rm'命令