Skip to content
Jordan Owens edited this page Jan 3, 2017 · 23 revisions

activerecord-import

activerecord-import is a library for bulk inserting data using ActiveRecord.

See the latest docs.

Why activerecord-import?

Because plain-vanilla, out-of-the-box ActiveRecord doesn’t provide support for inserting large amounts of data efficiently. With vanilla ActiveRecord you would have to perform individual save operations on each model:

10.times do |i|
  Book.create! :name => "book #{i}"
end

This may work fine if all you have is 10 records, but if you have hundreds, thousands, or millions of records it can turn into a nightmare. This is where activerecord-import comes into play.

An Introductory Example

To run examples on this page you’ll need to first require activerecord-import, for that please take a brief look at Requiring.

Here’s an example with equivalent behaviour using the #import method:

books = []
10.times do |i| 
  books << Book.new(:name => "book #{i}")
end
Book.import books

This call to import does whatever is most efficient for the underlying database adapter. Pretty slick, eh?

Recursive Example (PostgreSQL)

Here’s a recursive example using the #import method:

Assume that Books has_many Reviews

books = []
10.times do |i| 
  book = Book.new(:name => "book #{i}")
  book.reviews.build(:title => "Excellent")
  books << book
end
Book.import books, recursive: true

Features

Here’s a list of some of the high-level features that activerecord-import provides:

  • activerecord-import can work with raw columns and arrays of values (fastest)
  • activerecord-import works with model objects (faster)
  • activerecord-import can perform validations (fast)
  • activerecord-import can perform on duplicate key updates (requires MySQL or Postgres 9.5+)

Upgrading from ar-extensions

activerecord-import replaces the ar-extensions library and requires Rails 3. It provides the exact same API for importing data, but it does not include any additional ar-extensions functionality.

Clone this wiki locally