module

Ralph::BulkOperations::BulkOperationsMethods

Instance methods

delete_all(where conditions : NamedTuple) : Int64

Bulk delete records matching conditions

Deletes multiple records in a single DELETE statement without loading them into memory.

Parameters

  • where: Hash of column-value conditions (all conditions ANDed)

Returns

Number of records deleted (when supported by backend)

Example

# Delete all guest users
User.delete_all(where: {role: "guest"})

# Delete old posts
Post.delete_all(where: {status: "archived"})

Notes

  • Does NOT run callbacks (use destroy on instances if you need callbacks)
  • Does NOT handle dependent associations
  • For soft deletes, use update_all to set deleted_at instead
Source
delete_all(where conditions : Hash(String, DB::Any)) : Int64
Source
delete_all

Delete all records without conditions (DANGEROUS - deletes everything!)

Source
insert_all(records : Array(NamedTuple), returning : Bool = false) : BulkInsertResult

Bulk insert multiple records in a single query

Executes a multi-row INSERT statement for efficient batch inserts. This is significantly faster than inserting records one by one.

Parameters

  • records: Array of NamedTuples or Hashes with column-value pairs
  • returning: Whether to return inserted IDs (PostgreSQL only, default: false)

Returns

BulkInsertResult with count and optionally IDs

Example

result = User.insert_all([
  {name: "Alice", email: "alice@example.com"},
  {name: "Bob", email: "bob@example.com"},
])
puts result.count # => 2

Notes

  • Does NOT run validations or callbacks (bypasses model layer)
  • All records must have the same columns
  • Uses a single INSERT statement with multiple value tuples
Source
insert_all(records : Array(Hash(String, DB::Any)), returning : Bool = false) : BulkInsertResult
Source
update_all(updates : NamedTuple, where conditions : NamedTuple) : Int64

Bulk update records matching conditions

Updates multiple records in a single UPDATE statement without loading them into memory.

Parameters

  • updates: Hash of column-value pairs to set
  • where: Hash of column-value conditions (all conditions ANDed)

Returns

Number of records updated (when supported by backend)

Example

# Deactivate all guest users
User.update_all({active: false}, where: {role: "guest"})

# Set all posts to draft
Post.update_all({published: false, status: "draft"}, where: {author_id: 123})

Notes

  • Does NOT run validations or callbacks
  • Does NOT update timestamps automatically
Source
update_all(updates : Hash(String, DB::Any), where conditions : Hash(String, DB::Any)) : Int64
Source
update_all(updates : NamedTuple) : Int64

Update all records without conditions (use with caution!)

Source
update_all(updates : Hash(String, DB::Any)) : Int64
Source
upsert_all(records : Array(NamedTuple), on_conflict : Symbol | String | Array(Symbol) | Array(String), update : Array(Symbol) | Array(String) | Nil = nil, do_nothing : Bool = false) : BulkUpsertResult

Upsert (insert or update on conflict) multiple records

Performs an INSERT with ON CONFLICT handling. If a record with the same conflict key already exists, it updates the specified columns.

Parameters

  • records: Array of NamedTuples or Hashes with column-value pairs
  • on_conflict: Column(s) to check for conflicts (Symbol, String, or Array)
  • update: Columns to update on conflict (if nil, updates all non-conflict columns)
  • do_nothing: If true, skip conflicting records without updating

Returns

BulkUpsertResult with count and optionally IDs

Example

# Update name on email conflict
User.upsert_all([
  {email: "alice@example.com", name: "Alice Updated"},
  {email: "bob@example.com", name: "Bob Updated"},
], on_conflict: :email, update: [:name])

# Do nothing on conflict (INSERT IGNORE behavior)
User.upsert_all([
  {email: "alice@example.com", name: "Alice"},
], on_conflict: :email, do_nothing: true)
Source
upsert_all(records : Array(Hash(String, DB::Any)), on_conflict : Symbol | String | Array(Symbol) | Array(String), update : Array(Symbol) | Array(String) | Nil = nil, do_nothing : Bool = false) : BulkUpsertResult
Source