Rails 上传CSV数据

def upload(file_name)
  csv = IO.read(file_name).split("\n")

  csv[1..-1].map do |x|
    data = x.split(',')
    sid = data[0].gsub(/"/, '')
    puts sid
    current_term = data[1].gsub(/"/, '').chop
    number_of_payments = data[2].gsub(/"/, '').chop

    Account.find_by(id: sid)&.update_columns current_term: current_term, number_of_payments: number_of_payments
  rescue StandardError => e
    puts data
    puts e.message
    next
  end
end

upload('DOC-2021.csv')


发表评论

邮箱地址不会被公开。 必填项已用*标注