Method: Sequel::Dataset#multi_insert

Defined in:
lib/sequel/dataset/actions.rb

#multi_insert(hashes, opts = {}) ⇒ Object

This is a front end for import that allows you to submit an array of hashes instead of arrays of columns and values:

DB[:table].multi_insert([{:x => 1}, {:x => 2}])
# INSERT INTO table (x) VALUES (1)
# INSERT INTO table (x) VALUES (2)

Be aware that all hashes should have the same keys if you use this calling method, otherwise some columns could be missed or set to null instead of to default values.

You can also use the :slice or :commit_every option that import accepts.



376
377
378
379
380
# File 'lib/sequel/dataset/actions.rb', line 376

def multi_insert(hashes, opts={})
  return if hashes.empty?
  columns = hashes.first.keys
  import(columns, hashes.map{|h| columns.map{|c| h[c]}}, opts)
end