作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 will_paginate
使用一些复杂的查询并且无法正确计算总记录数(以显示适当数量的页面链接) - 即因为按多列分组。
因此,我打算获取用于检索所有记录的 SELECT 查询,而不实际执行它,并用 SELECT COUNT(*) FROM ...
包装它。手动,以获取记录数。
任何想法如何做到这一点?
编辑:我正在使用 Rails 2.3.x
最佳答案
对于 Rails 3:
在 Rails 3 docs 查看 ActiveRecord::Relation 文档.
# 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
关于ruby-on-rails - 如何在不实际执行的情况下获取由 ActiveRecord#find 创建的 SQL 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3814738/
我是一名优秀的程序员,十分优秀!