来自ActiveRecord :: Base.connection.execute(sql)的结果 - PostgreSQL

时间:2012-12-27 05:14:14

标签: sql ruby postgresql activerecord

如何在使用ActiveRecord :: Base Connection类执行SQL语句后找到PostgreSQL处理的记录数?

temp_sql = "UPDATE table_a SET column_a ='abc' WHERE column_b = 1"
result = ActiveRecord::Base.establish_connection(@db).connection.execute(temp_sql)

或者你可以建议更好的方法来做到这一点。请记住,上面的更新声明是一个简单的问题简要说明。我的实际查询是“基于集合”,涉及复杂的创建临时表,更新,插入语句。

1 个答案:

答案 0 :(得分:4)

在PG :: Result类中找到答案。它是cmd_tuples方法;

temp_sql = "UPDATE table_a SET column_a ='abc' WHERE column_b = 1"
result = ActiveRecord::Base.establish_connection(@db).connection.execute(temp_sql)
number_of_records = result.cmd_tuples