active_record_upsert
active_record_upsert copied to clipboard
Return number of inserted/updated records?
Is it possible to return the number of inserted records (or updated ones) as a result of executing the upsert statement?
For example, when the INSERT
succeeds, it could set an instance variable: __inserted: 1
. If there was a conflict and there was a ON CONFLICT DO UPDATE
, the number of updated records are set: __updated: 1
. If there was a WHERE
clause in the update which caused the update not to go through the value would be __updated: 0
.
I inspected the generated statement and I could see RETURNING *, xmax = 0
, so probably the last boolean can indicate insertion count?