My question is here is very specific. I have a collection of results from a Sunspot query,
@search = Product.search do
with(:website_id, id)
with(:archived_at, nil)
with(:category_id, restrictive_category_ids)
fulltext search_string do
phrase_fields(:name => 1, :artist => 3, :tag_list => 2)
minimum_match 0 #enables search to be performed by each word, not the whole phrase.
end
paginate :page => 1, :per_page => 2
end
@products = @search.results
This produces several pages of results, with 2 per page. All of this is verified in the console with Sunspot.
My problem is trying to use will_paginate with the results. For example, in the view:
<%= will_paginate @products, param_name: 'products', class: 'pagination-nav' %>
Since @products only returns 2 results in the initial query, will_paginate thinks there are only 2 results, and therefore doesn't even appear. If I replace @products with Product.all, it obviously appears and shows pages of results.
So I'm not sure what I need to do to tell will_paginate how many results there really are, so it knows how many pages to display. Sure, I could do a separate query for all these records, but that would be a major drag on the app and given that Sunspot claims to work with will_paginate, I am assuming there must be an easier way.
Thanks for your help!
I found the answer.
Instead of using @search.results within the "will_paginate" method in the view, I needed to use @search.hits.
using "hits" is key. "results" only returns the quantity of results from the paginated aspect in the model.search method. "hits", on the other hand, provides an array of records that reflects the quantity of the entire search, therefore allowing the paginated results to work properly.