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')

 

posted @ 2021-10-18 18:27  冰凌花花~  阅读(34)  评论(0编辑  收藏  举报