-
Notifications
You must be signed in to change notification settings - Fork 618
Home
activerecord-import is a library for bulk inserting data using ActiveRecord.
See the latest docs.
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.
_To run examples on this page you’ll need to first require activerecord-import, for that please take a brief look at [Requiring](https://github.com/zdennis/activerecord-import#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 # or use import!
This call to import does whatever is most efficient for the underlying database adapter. Pretty slick, eh?
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
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+)
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.