简体   繁体   中英

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. When trying to get the collection.count I am getting total number of records?

@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. 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 . If you want the total number of items the you should do @collection.count

You can try setting the page to last page to see the difference.

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.

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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