[Feature Proposal] ActiveRecord update_all/delete_all support use sql's returning clause statements

ActiveRecord’s update_all/delete_all methods is efficient, because it don’t need to instantiate object. But it only renurn number of changed rows at present, and sometimes I need to return some specific data, eg: id.

At this time, I can only execute the query statement once. Unfortunately, I cannot ensure that the query result is latest. Because after the query, there may be new data changes. This is terrible.

Alternatives. I can use exec_query method to execute sql for my purpose, but it is not elegant.

I hope update_all/delete_all can return specific data. eg:

User.returning(:id).delete_all === User.connection.exec_query('DELETE FROM "users" RETURNING "users"."id"')

PS: English is not my native language; please excuse typing errors.

1 Like

+1 for this feature. Frankly I’m a bit surprised it isn’t already available… this might be a bit difficult for the core team to pursue as upsert_all and insert_all already accept an option returning:

Maybe we could pursue adding that option to other AR methods that don’t instantiate objects: delete_all, update_all and update_columns and maybe others I’m missing?

1 Like