Jens Krämer

Pagination goodness

 |  ferret, ruby, rails

Finally implementing pagination for your acts_as_ferret search results is as easy as it should be (at least if you’re using aaf trunk, everybody else will have to wait for the soon-to-be-released 0.4.2):

@results = Model.find_with_ferret params[:query], :page => params[:page], :per_page => 10

Acts_as_ferret’s SearchResults class now gives you all you need to implement a helper rendering your pagination links: @results.page # => current page @results.page_count # => total number of pages @results.previous_page # => index of previous page or nil if on the first page @results.next_page # => index of next page or nil if on the last page

Best of all, this even works when you combine your query with ActiveRecord conditions.

Hint: really lazy people install the will_paginate plugin and use it’s will_paginate helper method to get their pagination links for free!

Comments

Mathias

Awesome! Was just about to sit down and think how I'd do that with AAF in our application.

Johnny Rodgers

Hi Jens,

First, thanks for acts_as_ferret. Made my first searchable Rails app much easier to accomplish than it might have been! I've just upgraded to aaf trunk to take advantage of the new pagination support, but I've run into a problem.

Pagination works perfectly as described above when using find_with_ferret on a single Model. However, I'm using the multi_search method as follows:

@search_results = Collection.multi_search(@query, [Artist, Label, Song])

Without pagination, this works perfectly, returning me a result set with records across the four searchable models of my application. I assumed that adding the :page and :per_page options to this query would give me the same pagination methods (.current_page, .total_pages, .next_page, etc) as the example using find_with_ferret:

@search_results = Collection.multi_search(@query, [Artist, Label, Song], :page => params[:page], :per_page => params[:per_page])

This doesn't throw an error, and still returns my results, but does not pass the :page and :per_page params to the result set. Inspecting the returned set in the log shows @current_page=1 and @per_page=19 (or however big the result set is). So, the :per_page option is not being recognized. Also, the default per_page value of 10 for Ferret results is applied, giving me a results page containing 10 results, though the @per_page variable is set to the size of the result set.

Any light you could shed on this would be great. I've been struggling with the different pagination methods out there for Ferret, but none of them want to play nicely with the results of multi_search queries. Please let me know if any other info would be useful.

Johnny Rodgers

Damn, none of the line breaks in my post were recognized. I'll try again using html tags:

--------------------------------------------------------------


Hi Jens,


First, thanks for acts_as_ferret. Made my first searchable Rails app much easier to accomplish than it might have been! I've just upgraded to aaf trunk to take advantage of the new pagination support, but I've run into a problem.


Pagination works perfectly as described above when using find_with_ferret on a single Model. However, I'm using the multi_search method as follows:


@search_results = Collection.multi_search(@query, [Artist, Label, Song])


Without pagination, this works perfectly, returning me a result set with records across the four searchable models of my application. I assumed that adding the :page and :per_page options to this query would give me the same pagination methods (.current_page, .total_pages, .next_page, etc) as the example using find_with_ferret:


@search_results = Collection.multi_search(@query, [Artist, Label, Song], :page => params[:page], :per_page => params[:per_page])


This doesn't throw an error, and still returns my results, but does not pass the :page and :per_page params to the result set. Inspecting the returned set in the log shows @current_page=1 and @per_page=19 (or however big the result set is). So, the :per_page option is not being recognized. Also, the default per_page value of 10 for Ferret results is applied, giving me a results page containing 10 results, though the @per_page variable is set to the size of the result set.


Any light you could shed on this would be great. I've been struggling with the different pagination methods out there for Ferret, but none of them want to play nicely with the results of multi_search queries. Please let me know if any other info would be useful.

Johnny Rodgers

Note, even when hard-coding values into the :page and :per_page options they are not passed in:


@search_results = Collection.multi_search(@query, [Artist, Label, Song], :page => 1, :per_page => 5)

jk

Hi Johnny, first of all sorry about the mess with the comment markup - I'll have to fix this one day...

Regarding your question - pagination support for multi_search is not implemented yet. I just created a ticket so I won't forget.