Crecto
Search…
Multi / Transactions
Crecto provides the Multi class, for utilizing database transactions.
With Multi, if any step of the transaction fails then all changes are rolled back.
As with Query, its easiest to create a shortcut variable.
1
Multi = Crecto::Multi
Copied!

Multi example and methods

First create the multi intance
1
multi = Multi.new
Copied!
Build the multi with the needed database operations
1
# Create the multi instance
2
multi = Multi.new
3
4
# Insert a new record
5
multi.insert(new_user)
6
7
# Delete an record
8
multi.delete(image)
9
10
# Delete all
11
multi.delete_all(Comment)
12
13
# Update a record
14
multi.update(post)
15
16
# Update all
17
multi.update_all(User, Query.where(name: "Shaquille"), {name: "RuPaul"})
Copied!
Insert the multi using your Repo
1
MyRepo.transaction(multi)
Copied!
Check the multi for errors. If there were errors on any of the operations, then none of the operations will be persisted.
1
if multi.errors.any?
2
puts "good to go"
3
else
4
puts "woops"
5
end
Copied!
Last modified 3yr ago
Copy link