Module: MassInsert::Base

Defined in:
lib/mass_insert/base.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#mass_insert(values, options = {}) ⇒ Object

MassInsert

Invoke mass insert just calling this from your ActiveRecord model…

User.mass_insert(values)

The values should be an array of hashes. Include attributes and values in every hash. Example…

values = [
  {:name   => "user name", :email  => "user email"},
  {:name   => "user name", :email  => "user email"}
]

Options

MassInset gem allow you to send it options as second param. Example…

User.mass_insert(values, options)

Primary key

By default primary key is ignored. If you wish primary key doesn’t be ignored you need to pass the primary_key option on true.

options = {:primary_key => true}

Each slice

Due you can get a database timeout error you can specify that the insertion will be in batches. You need to pass the each_slice option with the records per batch. Example…

User.mass_insert(values, :each_slice => 10000)


39
40
41
42
43
44
45
46
47
48
# File 'lib/mass_insert/base.rb', line 39

def mass_insert values, options = {}
  extend ClassMethods

  options[:class_name]  ||= self
  options[:each_slice]  ||= false
  options[:primary_key] ||= false

  @mass_insert_process = Process.new(values, options)
  @mass_insert_process.start
end