问题描述
我将 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 查询,而不实际执行它,并用 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
推荐答案
对于 Rails 3:
For Rails 3:
在 Rails 3 文档中查看 ActiveRecord::Relation 文档.
Check out the ActiveRecord::Relation docs at the Rails 3 docs.
# 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 语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!