Clear ORM
Search…
Bulk insert & delete
You can insert multiple models at the same time (using just one INSERT query) using the Collection#import method:
1
u = User.new({id: 1, first_name: "x"})
2
u2 = User.new({id: 2, first_name: "y"})
3
u3 = User.new({id: 3, first_name: "z"})
4
5
o = User.import([u, u2, u3])
Copied!
Only non-persisted valid models can be inserted. Any validation failure in the model list will throw an exception and revert the whole process.
Collection#import allows the passing optional block to refine he insert query built:
1
User.import([u, u2, u3]) do |request|
2
request.on_conflict("(id)").do_update { |upd|
3
upd.set("id = NULL")
4
.where { users.id == excluded.id }
5
}
6
end
Copied!
Copy link