rails runner script/...

在项目根目录中输入

rails runner script/import_efx.rb

script/import_efx.rb

file = File.open(File.join(Rails.root, 'script', 'efx.csv'))
count = 0
puts 'Input Price Data Start'
puts 'Wait...'

warehouse_name = {佛山仓: 1, 深圳仓: 2, 重庆仓: 3, 成都仓: 4, 天津仓: 5, 武汉仓: 6, 长沙仓: 7, 南京仓: 8, 杭州仓: 9,南宁仓: 10, 福州仓: 11, 海南仓: 12, 江阴仓: 13}

w = warehouse_name.with_indifferent_access
  file.each do |line| 
    array = line.force_encoding("UTF-8").split(",")
    r = User.find_by(employee_number: array[0].strip)
    if r.blank?
      record = User.find_or_initialize_by(employee_number: array[0].strip, 
                                                   mobile: array[3].strip,
                                          password_digest: array[1].strip,
                                                     salt: array[2].strip,
                                              platform_id: 3,
                                             warehouse_id: w["#{array[4].strip}"],
                                          customer_number: array[5].strip
                                         )
      record.save(validate: false)
      count += 1
      puts "#{count} rows"
    else
      count += 1
      r.update(customer_number: array[5].strip)
      r.save(validate: false)
      puts "#{count} rows"
    end
  end
file.close
puts 'Done.'

script/efx.csv
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值