Affected strings for ActiveRecord :: Base.connection.execute using Postgres

Is there a way to get the number of rows affected as a result of an SQL operation with ActiveRecord::Base.connection.execute?

I found this answer for MySQL adapters, but it does not work with Postgres.

Alternatively, if there is a way to get an SQL text response (for example, β€œUPDATE 126”), this will also work.

+2
source share
1 answer

You can use the method cmd_tuples:

sql = "UPDATE users SET updated_at = '#{DateTime.now}' WHERE id = 1"
ActiveRecord::Base.connection.execute(sql).cmd_tuples
# => 1

Documentation: http://www.rubydoc.info/gems/pg/0.17.1/PG%2FResult:cmd_tuples

+2
source

Source: https://habr.com/ru/post/1568191/


All Articles