Skip to content

Instantly share code, notes, and snippets.

@chelseatroy
Last active September 20, 2018 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chelseatroy/f43b00c27bd91046be127bd33670114a to your computer and use it in GitHub Desktop.
Save chelseatroy/f43b00c27bd91046be127bd33670114a to your computer and use it in GitHub Desktop.
Example: Transactional Data Import
class Import
def import_workouts(rows)
errors = []
CSV.foreach(@import_file_path, {headers: true}) do |row|
begin
attributes = row.to_hash
row.create(attributes)
errors += row.failures
rescue StandardError => e
errors << [row.index, row.data, [e.message, *e.backtrace]]
end
end
File.open('import_error_report', 'w') { |file| file.write(errors.join("\n") }
end
...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment