Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

Aborting the import process

Ernesto Garcia edited this page Feb 23, 2014 · 4 revisions

Sometimes it is desirable to have the importer abort the process before it reaches the end of the data. This is achieved by invoking the importer's abort! method while processing a row.

It's important to note that aborting doesn't affects the data imported so far. It only instructs the importer to stop processing any remaining data. For aborting so that all the data imported is rolled back, try using a transactional importer.

Let's look at the details of aborting the import process. Take the following example:

class EmployeeImporter < ActiveImporter::Base
  imports Employee

  on :row_processing do
    abort! if row['Name'] == 'ABORT'
    # ...
  end
end

As shown above, this method will usually be invoked from within the :row_processing event callback, if some condition occurs. It can also be invoked from within any other callback blocks. For instance, one could create an importer that aborts the process as soon as an error occurs.

class EmployeeImporter < ActiveImporter::Base
  imports Employee

  on :row_error do
    abort!
  end

  # ...
end