Ralph::BulkOperations::BulkOperationsMethods
Instance methods
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_allto set deleted_at instead
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 pairsreturning: 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
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 setwhere: 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
Update all records without conditions (use with caution!)
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 pairson_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)