0

我有以下代码大部分都在工作。但是我确实收到了无法保存的错误消息,因为邮件地址已经存在。当我先做一个 destroy_all 时,这怎么可能?

我的猜测:有多个行具有相同的邮件地址(这是真的),并且不知何故,它似​​乎并没有破坏那些通过下面的过程插入的行。

有没有办法强迫它也考虑到这些?

namespace :db do

  desc "load vdc members from google CSV"
  task :load_csv_data  => :environment do
    require 'csv'

    CSV.foreach("data.csv",{:headers=>:first_row}) do |row|

      User.destroy_all(:email => row[7])

      user = User.new(
          :password => "temppwd",
          :name => row[1],
          :first_name => row[2],
          :birth_date => row[3],
          :street => row[4],
          :zipcode => row[5],
          :town => row[6],
          :email => row[7],
          :certificate_id => Certificate.find_by_name(row[9]).id,
          :external_certificates => row[11],
          :education_level => row[12],
          :phone_number => row[15]
      )
      if user.save
        puts "saved " + row[1] + " " + row[2]
      else
        puts "Could not save " + row[1] + " " + row[2] + " due to "
        puts user.errors.full_messages.first if user.errors.any?
      end
    end
  end
end
4

0 回答 0