如何访问由update_all生成的原始 SQL 语句(活动记录方法)



我只是想知道是否有办法访问为update_all ActiveRecord 请求执行的原始 SQL。例如,以下面的简单示例为例:

Something.update_all( ["to_update = ?"], ["id = ?" my_id] )

在 rails 控制台中,我可以看到原始 SQL 语句,所以我猜我可以以某种方式访问它?

PS - 我对update_all特别感兴趣,无法将其更改为其他任何东西。

谢谢!

如果你看一下update_all的实现方式,你不能像在关系上那样调用to_sql,因为它直接执行并返回一个整数(执行的行数(。

除了复制整个方法并更改最后一行之外,无法进入流程或获得所需的结果:

module ActiveRecord
# = Active Record Relation
class Relation
def update_all_to_sql(updates)
raise ArgumentError, "Empty list of attributes to change" if updates.blank?
if eager_loading?
relation = apply_join_dependency
return relation.update_all(updates)
end
stmt = Arel::UpdateManager.new
stmt.set Arel.sql(@klass.sanitize_sql_for_assignment(updates))
stmt.table(table)
if has_join_values? || offset_value
@klass.connection.join_to_update(stmt, arel, arel_attribute(primary_key))
else
stmt.key = arel_attribute(primary_key)
stmt.take(arel.limit)
stmt.order(*arel.orders)
stmt.wheres = arel.constraints
end
#- @klass.connection.update stmt, "#{@klass} Update All"
stmt.to_sql
end
end
end

您看到日志语句的原因是,连接在执行语句时会记录这些语句。虽然您可以覆盖日志记录,但实际上不可能对来自单个 AR 方法的调用执行此操作。

如果你已经设置了RAILS_LOG_LEVEL=debugRails 会显示它执行的 SQL 语句。

# Start Rails console in debug mode
$ RAILS_LOG_LEVEL=debug rails c
# Run your query
[1] pry(main)> Something.update_all( ["to_update = ?"], ["id = ?" my_id] )
SQL (619.8ms) UPDATE "somethings" WHERE id = 123 SET to_update = my_id;
# ^it prints out the query it executed

最新更新