module FastInserter
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, group_size: 1_000 }, 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. Default 10_000.
Constants
- VERSION