I’m importing 17 csv files into my app via rake tasks. The other files will import fine and not create extra rows, but for some reason this file is creating about 60 empty rows in my database. The information I’m trying to import is going in fine and it’s being associated with it’s appropriate tables the way it should. So if you were to use the app, everything functions properly but its making a mess of my table.
For example, I’m importing 2 rows into the table and right after those two are being created, it’s followed by 58 blank rows. Does anyone have any suggestions about how I can use a conditional statement to keep this from happening and please be specific I’m still relatively new at this. Thanks
Here is my code:
require 'csv'
namespace :import_mat_lists_csv do
task :create_mat_lists => :environment do
puts "Import Material List"
csv_text = File.read('c:/rails/thumb/costrecovery/lib/csv_import/mat_lists.csv')
csv = CSV.parse(csv_text, :headers => true)
csv.each_with_index do |row,index|
row = row.to_hash.with_indifferent_access
MatList.create!(row.to_hash.symbolize_keys)
matlist = MatList.last
if @report_incident_hash.key?(matlist.report_nr)
matlist.incident_id = "#{@report_incident_hash[matlist.report_nr]}"
end
#matlist.timesheet_id = @timesheet_id_array[index]
matlist.save
end
end
end
There are a few ways to handle this, but perhaps the easiest would be to add:
Your code would read: