4

Is there a way to get the number of affected rows resulting from a SQL operation using ActiveRecord::Base.connection.execute?

I found this answer for MySQL adapters, but it doesn't work with Postgres.

Alternatively, if there's a way to get the SQL text response (e.g. "UPDATE 126"), that would work too.

1 Answer 1

5

You can use cmd_tuples method:

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.