-
Notifications
You must be signed in to change notification settings - Fork 0
Home
activerecord-import is a library for bulk inserting data using ActiveRecord. Note: activerecord-import requires Rails 3.×. It does not work with Rails 2.×.
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.
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?
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)
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.