1
votes

I am working on a Rails project that has pagination set up for venues (through thinking sphinx), and I am trying to set it up for users. I tried to mimic the functionality of venues as closely as I could. The problem I am running into is I am only getting back the number of results specified by :per_page instead of the total results for the query. If I increase :per_page to more then the total results, I get all of them to show up and the number links for pagination but they don't work.

In my controller I am doing

@gender = params[:user][:gender] unless params[:user].blank?
@ethnicity = params[:user][:ethnicity] unless params[:user].blank?
@age = params[:user][:age] unless params[:user].blank?
@page = params[:page].blank? ? 1 : params[:page]
@users = User.matches_for_admin({ 
  :gender => @gender,
  :ethnicity => @ethnicity,
  :age => @age,
  :relationship => @relationship,
  :per_page => 20,
}, @page)
@pages = @users[:count] / 20 + ((@users[:count] % 20) > 0 ? 1 : 0)

if I set the :per_page to 200 I get all my results, and links come up for the pages (for pagination) but the links don't work because the per_page is higher then the total results.

This is the helper function I am using for pagination

  def pagination_user(current_page, pages)
    return if pages == 0
    render :partial => 'pagination_user', :locals => { :current_page => current_page.to_i, :pages => pages }
  end

And this is the partial that sets up the number links

<div class="fullWidthPanel lightFilter pagination">
  <ul>
    <li> Pages: </li>
    <% (1..pages).each do |page| %>
      <li>
      <% if page == current_page %>
        <%= page %>
      <% else %>
        <a href="<%= url_for_user_page page %>">
          <%= page %>
        </a>
      <% end %>
      </li>
    <% end %>
  </ul>
  <span class="results">
    <strong>
    Showing results <%= 1 + (current_page-1)*20 %> 
    to <%= [current_page*20, @users[:count]].min %>
    of <%= @users[:count] %>
    </strong>
  </span>
</div>

Where url_for_user_page just sets up the url to have the search options for the pagination to work.

I am new with thinking sphinx and I am confused why it is working for venues and not users when I have them set up the same. It seems like the amount of results I get are based on the :per_page attribute, instead of the number of results I actually get back from the query. Any insight into what potential issues I may be having or something I am over looking would be appreciated.

Thanks,

Alan

3

3 Answers

0
votes

Nvm I figured it out. The number of bars found in the search was being taken from the return results from the sphinx search (based on the per_page). I had to have the count for the pagination be based on:

:count => User.search_count("", search_options) 

so that it gets all the possible results instead of just the results being found with sphinx (which is based on the per_page value)

0
votes

Let me tell you what I did. After I search with sphinx I get the id of the records as an array and then I search using .where() using the ids

ids = User.search(params[:search_query]).collect{|c| c.id} #will return #==> [1,2,3]
@users = User.where(:id => ids).paginate(:per_page => 10, :page => params[:page])
0
votes

If you are using sphinx search with page options (page, per_page) you will get the count of the paginated result. So for the following code you will get count = 20 (same as per_page count) even though the result has more than 20 records.

users_count = User.search(query, :per_page => 20, :page => params[:page]).populate.count 

So use 'total_entries' on sphinx result to get the total record count

users = User.search(query, :per_page => 20, :page => params[:page]).populate
users_count = users.total_entries