will_paginate 报告条目和页面过多 [英] will_paginate reporting too many entries and pages

查看:29
本文介绍了will_paginate 报告条目和页面过多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 will_paginate 显示从包含连接和选择语句的查询返回的数据.当我对数据进行分页时,条目数等于执行 select 语句之前的条目数,即使在查询之后调用 paginate 并且查询包含的元素少于 paginate 报告.

I'm using will_paginate to display data returned from a query that includes both a joins and a select statement. When I paginate the data the number of entries is equal to the number of entries before executing the select statement, even though paginate is being called after the query, and the query contains fewer elements than paginate reports.

@sales = Sale.joins(:line_items).where(company_id: company_id, status: ['Complete', 'Voided'], time: (midnight_1..midnight_2)).order('id DESC')
puts @sales.length

14

@sales = @sales.select('distinct sales.*')
puts @sales.length

4

@sales.paginate(:per_page => 4, :page => params[page])
puts @sales.total_entries

14

这会导致显示指向空页面的链接.

This leads to displaying links to empty pages.

推荐答案

分页和加入 has_manyhas_and_belongs_to_many 与 will_paginate 的关联总是会稍微困难一些,或者确实是任何分页解决方案.

It's always going to be slightly harder to paginate and join in has_many or has_and_belongs_to_many associations with will_paginate, or indeed any pagination solution.

如果您不需要查询已加入的关联,您可以将其删除.您失去了在一次查询中获取关联订单项的好处,但不会损失太多.

If you don't need to query on the joined in association you can remove it. You lose the benefit of getting the associated line items in one query but you don't lose that much.

如果您需要查询它,并且大概您想要只有订单项的销售,您需要将 :count 选项传递给 paginate 调用,该选项指定用于调用的其他选项以计算如何有很多项目.在你的情况下:

If you need to query on it, and presumably you want sales that only have line items, you'll need to pass in a :count option to the paginate call which specifies additional options that are used for the call to count how many items there are. In your case:

@sales.paginate(:per_page => 4, 
    :page => params[page], 
    :count => {:group => 'sales.id' })

这篇关于will_paginate 报告条目和页面过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆