title: What is the Optimized way to Paginate Active Record Objects with Filter?
body: <p>I want to display the Users list in pagination with my rails API, However I have few constraints here before displaying the users I want to check users who have access to the view files, Here is the code:</p>
  <pre><code>  def verified_client
      conditions = {}
      conditions[:user_name] = fetch_verified_users_with_api_call # returns[user_1,user_2, ....]
      @users = User.where(conditions).where('access NOT LIKE ?', 'admin_%').ordered
      will_paginate(@users, params[:page])
    end
  </code></pre>
  <p>Q1) Is there a way where I don't have to make sql call when users try to fetch subsequent pages(page 2, page 3.. page n)?</p>
  <p>Q2) What would happen when verified_users list return million on items? I suspect the SQL will fail</p>
  <p>I could have used limit and offset with the Query, but I will not know the total result and page size to achieve the same I have to fire one more SQL call to get count and write up own logic to get number of pages.</p>
  <p>Generated SQL:</p>
  <pre><code>select  *
      from  users
      where  user_name IN (user_1, user_2 .... user_10000)
        AND  (access NOT LIKE 'admin_%')
  </code></pre>