will_paginate and .sort =>

蓝咒 提交于 2020-01-25 05:36:49

问题


I recently got the will_paginate gem installed and it works great, but I would like it to work with the setup I currently have. Right now I show all feeds (or posts) on the same page, but they are sorted after how many people clicked on that page:

controller.rb

 @t = Time.now
 @h1 = @t - 1.hour
 @feeds = Feed.find(:all, :order => "created_at DESC").sort { |p1, p2| p2.impressionist_count(:filter=>:all, :start_date=>@h1) <=> p1.impressionist_count(:filter=>:all, :start_date=>@h1)}

Now... I tested the paginate gem and it works fine if I do this in the controller:

controller.rb

 @feeds = Feed.paginate(:per_page => 10, :page => params[:page], :order => "created_at DESC")

So I thought 1+1=2 and tried to combine the two by doing:

controller.rb

@feeds = Feed.paginate(:per_page => 10, :page=>params[:page], :order => "created_at DESC").sort { |p1, p2| p2.impressionist_count(:filter=>:all, :start_date=>@h1) <=> p1.impressionist_count(:filter=>:all, :start_date=>@h1)}

I'm not able to sort my posts and paginate them. I get an error when I try to load the page:

undefined method `total_pages' for #

I would like this to work, it would be pretty sweet :). However, if it does not work, is there any other way of doing this?

Thanks in advance!


回答1:


It's because will_paginate works by default only on ActiveRecord relationships. Once you use the sort, it's converted to an array. If you want will_paginate to work with an array, you'll need to require support for it in your controller.

require 'will_paginate/array' 


来源:https://stackoverflow.com/questions/14552999/will-paginate-and-sort

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!