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 返回数组?没有屈服函数。从逻辑上讲,它只会返回最后一个元素
有sheet.map方法为用户返回地图