Module: FastInserter

Defined in:
lib/fast_inserter/fast_inserter_base.rb,
lib/fast_inserter.rb,
lib/fast_inserter/version.rb

Overview

Highly based off of github.com/sportngin/m2m_fast_insert Unfortunately, that gem was not up to date for rails 4.

NOTE:

- Only 'static_column' values and 'values' are sanitized for SQL injection.

Example usage:

@mass_email = MassEmail.find(params[:id])
user_ids = [1, 2, 3, 4] # ids to fast insert
params = {
  table: 'mass_emails_users',
  static_columns: {
    mass_email_id: @mass_email.id
  },
  additional_columns: {
    created_by_id: current_user.id
  },
  options: {
    timestamps: true,
    unique: true,
    check_for_existing: true
  },
  variable_column: 'user_id',
  values: user_ids
}
inserter = FastInserter::Base.new(params)
inserter.fast_insert

Options:

unique: true
  Makes sure all values are unique
check_for_existing: true
  Checks if values already exist in the database and only inserts nonexisting values
  This checks values scoped to static columns.
timestamps: true
  Adds created_at and updated_at columns to insert statement
additional_columns: Hash
  A hash representing additional column values to set that you don't want
  to include in uniqueness checks or other pre-insertion operations.
group_size: Integer
  The number of items you want to insert per batch of records. Defaults to 1_000.

Defined Under Namespace

Classes: Base

Constant Summary collapse

VERSION =
'2.0.0'.freeze