Will_paginate with mongoid (total_entries issue)

2019-09-18 10:26发布

问题:

When using mongoid with 'will_paginate' gem, it seems like method called 'total_entries' doesnt work which can affect perfomance

回答1:

So I was using Mongoid with 'will_paginate' gem and I had this problem with 'total_entries' option, and to be more specific, I dont think it was working at all, which is not very good for perfomance in many situations... After a while I found this gem but it didnt look like 'total_entries' method was working there so here's what I did:
1) Opened mongoid_paginator in will_paginate_mongoid location
2) Made a little fix, so it looked like this:

WillPaginate::Collection.create(options[:page], options[:per_page]) do |pager|
      items_count = options[:total_entries] || self.count
      fill_pager_with self.skip(options[:offset]).limit(options[:per_page]), items_count, pager

which I took from here and hallelujah, it works again, no more counts on every load.

Since I am a newbie I could make something foolish and I hope I will be corrected if so and hope it'll be helpful for somebody :)