class ActiveRecord::Base

def import(*args)

* ids - the priamry keys of the imported ids, if the adpater supports it, otherwise and empty array.
* num_inserts - the number of insert statements it took to import the data
* failed_instances - an array of objects that fails validation and were not committed to the database. An empty array if no validation is performed.
This returns an object which responds to +failed_instances+ and +num_inserts+.
= Returns

BlogPost.import columns, attributes, :on_duplicate_key_update=>{ :title => :title }

model. Below is an example:
control over what fields are updated with what attributes on your
to model attribute name mappings. This gives you finer grained
The :on_duplicate_key_update option can be a hash of column name

==== Using A Hash

BlogPost.import columns, values, :on_duplicate_key_update=>[ :date_modified, :content, :author ]

a duplicate record is found. Below is an example:
names. The column names are the only fields that are updated if
The :on_duplicate_key_update option can be an array of column

==== Using an Array

The :on_duplicate_key_update option can be either an Array or a Hash.

== On Duplicate Key Update (MySQL only)

puts posts.first.persisted? # => true
BlogPost.import posts, :synchronize => posts, :synchronize_keys => [:title]
posts = [BlogPost.new(:title => "Foo"), BlogPost.new(:title => "Bar")]
# Example synchronizing unsaved/new instances in memory by using a uniqued imported field

puts post.author_name # => 'yoda'
BlogPost.import posts, :synchronize=>[ post ]
values = [ [ 'yoda', 'test post' ] ]
columns = [ :author_name, :title ]
puts post.author_name # => 'zdennis'
post = BlogPost.where(author_name: 'zdennis').first
# Example synchronizing existing instances in memory

BlogPost.import( columns, values, :validate => false )
values = [ [ 'zdennis', 'test post' ], [ 'jdoe', 'another test post' ] ]
columns = [ :author_name, :title ]
# Example using column_names, array_of_value and options

BlogPost.import columns, values
values = [ [ 'zdennis', 'test post' ], [ 'jdoe', 'another test post' ] ]
columns = [ :author_name, :title ]
# Example using column_names and array_of_values

BlogPost.import posts
BlogPost.new :author_name=>'Zach Dennis', :title=>'AREXT3' ]
BlogPost.new :author_name=>'Zach Dennis', :title=>'AREXT2',
posts = [ BlogPost.new :author_name=>'Zach Dennis', :title=>'AREXT',
# Example using array of model objects

class BlogPost < ActiveRecord::Base ; end
== Examples

objects.
if the adapter supports setting the primary keys of the newly imported
* +recursive - true|false, tells import to import all autosave association
(if false) even if record timestamps is disabled in ActiveRecord::Base
* +timestamps+ - true|false, tells import to not add timestamps \
existing model instances in memory with updates from the import.
that you are currently importing data into. This synchronizes
* +synchronize+ - an array of ActiveRecord instances for the model
Key Update below.
use MySQL's ON DUPLICATE KEY UPDATE ability. See On Duplicate\
* +on_duplicate_key_update+ - an Array or Hash, tells import to \
ActiveRecord validations. Validations are enforced by default.
* +validate+ - true|false, tells import whether or not to use \
== Options

below for what +options+ are available.
parameter, +options+, is a hash. This is optional. Please see
The first two parameters are the same as the above form. The third

==== Model.import column_names, array_of_values, options

the order of the +column_names+.
record. The order of values in each subarray should match up to
arrays. Each subarray is a single set of values for a new
The second parameter, +array_of_values+, is an array of

strings which specify the columns that you want to update.
The first parameter +column_names+ is an array of symbols or

==== Model.import column_names, array_of_values

objects that you want updated.
With this form you can call _import_ passing in an array of model

==== Model.import array_of_models

Model.import column_names, array_of_values, options
Model.import column_names, array_of_values
Model.import array_of_models
== Usage

performing the import.
the ActiveRecord::Callbacks during creation/modification while
This can be used with or without validations. It does not utilize

inserted.
about having ActiveRecord objects returned for each record
you want to create more than one record at a time and do not care
ActiveRecord::Base#save multiple times. This method works well if
This is more efficient than using ActiveRecord::Base#create or

Imports a collection of values to the database.
def import(*args)
  if args.first.is_a?( Array ) and args.first.first.is_a? ActiveRecord::Base
    options = {}
    options.merge!( args.pop ) if args.last.is_a?(Hash)
    models = args.first
    import_helper(models, options)
  else
    import_helper(*args)
  end
end