Module: BulkInsertSafe


A mixin for ActiveRecord models that enables callers to insert instances of the target class into the database en-bloc via the [bulk_insert] method.

Upon inclusion in the target class, the mixin will perform a number of checks to ensure that the target is eligible for bulk insertions. For instance, it must not use ActiveRecord callbacks that fire between [save]s, since these would not run properly when instances are inserted in bulk.

The mixin uses ActiveRecord 6’s [InsertAll] type internally for bulk insertions. Unlike [InsertAll], however, it requires you to pass instances of the target type rather than row hashes, since it will run validations prior to insertion.


class MyRecord < ApplicationRecord
  include BulkInsertSafe # must be included _last_ i.e. after any other concerns

# simple

# with custom batch size
MyRecord.bulk_insert!(items, batch_size: 100)

# without validations
MyRecord.bulk_insert!(items, validate: false)

# with attribute hash modification
MyRecord.bulk_insert!(items) { |item_attrs| item_attrs['col'] = 42 }

Constant Summary collapse


These are the callbacks we think safe when used on models that are written to the database in bulk

MethodNotAllowedError =
PrimaryKeySetError =