Examples

Alex Ghiculescu edited this page Jul 29, 2017 · 11 revisions

For information about requiring activerecord-import in your project please see Requiring.

Import Using Columns and Arrays

The #import method can take an array of column names (string or symbols) and an array of arrays. Each child array represents an individual record and its list of values in the same order as the columns. This is the fastest import mechanism and also the most primitive.

columns = [ :title, :author ]
values = [ ['Book1', 'FooManChu'], ['Book2', 'Bob Jones'] ]

# Importing without model validations
Book.import columns, values, :validate => false

# Import with model validations
Book.import columns, values, :validate => true

# when not specified :validate defaults to true
Book.import columns, values

Import Using Hashes

The #import method can take an array of hashes. The keys map to the column names in the database.

values = [{ title: 'Book1', author: 'FooManChu' }, { title: 'Book2', author: 'Bob Jones'}]

# Importing without model validations
Book.import values, validate: false

# Import with model validations
Book.import values, validate: true

# when not specified :validate defaults to true
Book.import values

Import Using Hashes and Explicit Column Names

The #import method can take an array of column names and an array of hash objects. The column names are used to determine what fields of data should be imported. The following example will only import books with the :title field:

books = [ 
  {title: "Book 1", author: "FooManChu"},
  {title: "Book 2", author: "Bob Jones"}
]
columns = [ :title ]

# without validations
Book.import columns, books, validate: false

# with validations
Book.import columns, books, validate: true

# when not specified :validate defaults to true
Book.import columns, books

# result in table books
# title  | author
#--------|--------
# Book 1 | NULL   
# Book 2 | NULL   

Import Using ActiveRecord Models

The #import method can take an array of models. The attributes will be pulled off from each model by looking at the columns available on the model.

books = [ 
  Book.new(:title => "Book 1", :author => "FooManChu"),
  Book.new(:title => "Book 2", :author => "Bob Jones")
]

# without validations
Book.import books, :validate => false

# with validations
Book.import books, :validate => true

# when not specified :validate defaults to true
Book.import books

Import Using ActiveRecord Models and Explicit Column Names

The #import method can take an array of column names and an array of models. The column names are used to determine what fields of data should be imported. The following example will only import books with the :title field:

books = [ 
  Book.new(:title => "Book 1", :author => "FooManChu"),
  Book.new(:title => "Book 2", :author => "Bob Jones")
]
columns = [ :title ]

# without validations
Book.import columns, books, :validate => false

# with validations
Book.import columns, books, :validate => true

# when not specified :validate defaults to true
Book.import columns, books

# result in table books
# title  | author
#--------|--------
# Book 1 | NULL   
# Book 2 | NULL   

Import and batching

The #import method can take a :batch_size option to control the number of rows to insert per INSERT statement. The default is the total number of records being inserted so there is a single INSERT statement.

books = [ 
  Book.new(:title => "Book 1", :author => "FooManChu"),
  Book.new(:title => "Book 2", :author => "Bob Jones"),
  Book.new(:title => "Book 1", :author => "John Doe"),
  Book.new(:title => "Book 2", :author => "Richard Wright")
]
columns = [ :title ]

# 2 INSERT statements for 4 records
Book.import columns, books, :batch_size => 2
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.