问题
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