Ruby on Rails 批量导入

问题描述 投票:0回答:1

https://github.com/bodrovis-learning/RailsSeriesYT/blob/lesson_11/app/services/user_bulk_service.rb第15行和26行

def call
Zip::File.open(@archive) do |zip_file|
  zip_file.glob('*.xlsx').each do |entry|
    User.import users_from(entry), ignore: true
  end
end

def users_from(entry)
    sheet = RubyXL::Parser.parse_buffer(entry.get_input_stream.read)[0]
    sheet.map do |row|
      cells = row.cells[0..2].map { |c| c&.value.to_s }
      User.new name: cells[0],
               email: cells[1],
               password: cells[2],
               password_confirmation: cells[2]
    end

users_from 在循环中返回 user 时如何为 User.import 返回数组?没有屈服函数。从逻辑上讲,它只会返回最后一个元素

ruby-on-rails ruby ruby-on-rails-4
1个回答
0
投票

有sheet.map方法为用户返回地图

© www.soinside.com 2019 - 2024. All rights reserved.