如何获取由ActiveRecord#find创建的SQL语句而不实际执行它? [英] How can I get SQL statement created by ActiveRecord#find without actually executing it?
本文介绍了如何获取由ActiveRecord#find创建的SQL语句而不实际执行它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 will_paginate
与一些复杂的查询,它无法正确计算总记录数(为了显示适当数量的网页链接) - 也就是因为
I am using will_paginate
with some complicated queries and it is unable to correctly calculate number of total records (in order to display proper number of page links) - namely because of grouping by multiple columns.
因此,我打算获得SELECT查询,用于检索ALL记录,而不实际执行它,并用 SELECT COUNT(*)FROM ...
手动,以获取记录数。
So, I am intending to get the SELECT query which would be used to retrieve ALL records, without actually executing it, and wrap it with SELECT COUNT(*) FROM ...
manually, in order to get the number of records.
编辑:我使用Rails 2.3.x
I am using Rails 2.3.x
推荐答案
p>对于Rails 3:
For Rails 3:
# get the relation
rel = User.complex_scope.chained_complex_scope
# get the SQL
# this does not execute the query
sql = rel.to_sql
# find out how many records
# this executes the query behind the scenes
count = rel.size
这篇关于如何获取由ActiveRecord#find创建的SQL语句而不实际执行它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文