Search code examples
ruby-on-railspaginationdepagination

How to un-paginate a paginated object by will_paginate?


@models is a paginated object with gem [will_paginate][1]:

 @models = @models.page(params[:page]).per_page(50)

Then retrieving the ids:

id_array = @models.pluck('id')

But there are only first 50 ids plucked in id_array because @models is paginated. Is there a way to un-paginate the @models?


Solution

  • If you know that @models contains User records:

    User.scoped.pluck(:id)
    

    If you don't know what contains @models:

    @models.first.try(:class).try(:pluck, :id)
    

    But the second option will only work if you have at least one record in the @models array, also if @models contains several Model's records (example: User and Player record), it won't take all IDs of both models but only one.