Last active
March 10, 2024 19:13
-
-
Save k41n3w/4aa62a3b7dfc34ccaab270e99cf71a0b to your computer and use it in GitHub Desktop.
importando-dados-com-rails-na-velocidade-da-luz-3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
require 'activerecord-import' | |
file_path = 'caminho/para/seu/arquivo.csv' | |
users_data = [] | |
CSV.foreach(file_path, headers: true) do |row| | |
users_data << User.new(name: row['name'], email: row['email']) | |
end | |
# Inserção em lote com validações | |
results = User.import users_data, validate: true | |
if results.failed_instances.empty? | |
puts "Todos os registros foram importados com sucesso." | |
else | |
puts "Alguns registros falharam a validação e não foram importados." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment