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