简体   繁体   English

使用will_paginate gem获取记录在rails中的当前页面

[英]Getting the records count at current page in rails with will_paginate gem

I need to get the number of records at current page. 我需要获取当前页面的记录数量。 I am using will_paginate gem for pagination. 我使用will_paginate gem进行分页。 When trying to get the collection.count I am getting total number of records? 在尝试获取collection.count时,我获得了总记录数?

@collection = User.paginate(page: params[:page], per_page: 10).all

when I try to get the count of collection it returns the total entries. 当我尝试获取收集计数时,它返回总条目。 How to get the records count of a current table 如何获取当前表的记录数

尝试:

@collection.total_entries

If u need total entries in entire query: 如果您需要整个查询中的总条目:

@collection.total_entries

If u need entry count in current page: 如果您需要当前页面中的输入计数:

@collection.size

Meanwhile u dont need to call last .all scope in pagination. 同时你不需要在分页中调用最后的.all范围。 Just try: 你试一试:

@collection = User.paginate(page: params[:page], per_page: 10)

If you want the current number of items on current page the you should do @collection.size . 如果你想要当前页面上的当前项目数,你应该做@collection.size If you want the total number of items the you should do @collection.count 如果你想要你应该做的项目总数@collection.count

You can try setting the page to last page to see the difference. 您可以尝试将page设置为最后一页以查看差异。

Also use 也用

@collection = User.paginate(page: params[:page], per_page: 10)

as the paginate will return only the corresponding set, configured with page and per_page params. 因为paginate只返回相应的set,配置了pageper_page参数。

I also wanted to find the number of records for the current page. 我还想找到当前页面的记录数。 But @surveys.size is not returning the number of records in the current page, instead, it's returning the number of records if the current page had been filled. 但@ surveys.size没有返回当前页面中的记录数,而是返回当前页面已填满的记录数。 Rails 5.2.2 ruby​​ 2.5.1p57(2018-03-29 Revision 63029)[x86_64-linux]

@collection.length 

is the one that provides the needed result. 是提供所需结果的那个。

@collection.entries

is giving you the entries on the current page. 正在为您提供当前页面上的条目。 If you for example paginate 25 entries, it will show the 25 entries in an Array class 例如,如果您分页25个条目,它将显示Array类中的25个条目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM