def self.import file, organization
counter = 0
CSV.foreach(file.path, encoding: 'windows-1251:utf-8', headers: true) do |row|
name = (row["First Name"].to_s + " " + row["Last Name"].to_s).titleize
customer = Customer.create(
name: name,
phone: row["Main Phone"],
email: row["Main Email"],
address: row["Address"],
repair_shop: repair_shop
)
puts "#{name} - #{customer.errors.full_messages.join(',')}" if customer.errors.any?
counter += 1 if customer.persisted?
end
message = "Imported #{counter} users."
end
This is the code I have so far. I'm importing files with 10,000 rows, so it overwhelms my production server in processing.
How could I do this in batches?