Ok, Ferret is great, but how to paginate?

I saw several solutions on the net and liked this one the most.

It appears that it’s quite easy to integrate acts_as_ferret with will_paginate, the officially recommended pagination plugin for Rails.

First, a patch is needed. I placed it to a new file in my special place for patches, vendor/plugins/patches/lib; it could also go in some of existing application files, e.g. bottom of environment.rb:


module ActsAsFerret
  module ClassMethods
    alias :find_all_by_contents :find_by_contents
  end
end

Then, in controller:


@users = User.paginate_by_contents(@search.query,
  :total_entries => User.total_hits(@search.query), :page => params[:page],
  :per_page => 10)

Change User to your model class here. You can also use sorting here, see Tutorial below for guidelines.

The trick is that will_paginate is friendly to model class’s methods like find_by_xxx; acts_as_ferret’s find_all_by_contents’s name is a bit wrong for it, so we make an alias, and later this method becomes our model’s method, when we put acts_as_ferret statement in the model class definition.

References:

  • Acts_As_Ferret Tutorial
  • This solution was taken from will_paginate’s author blog, thanks ocher!

No Responses to “acts_as_ferret pagination”  

  1. No Comments

Leave a Reply