# Complex pagination issue...

**URL:** <https://discuss.rubyonrails.org/t/complex-pagination-issue/29515>\
**Category:** rubyonrails-talk\
**Created:** [May 19, 2008, 4:37am UTC](https://discuss.rubyonrails.org/t/complex-pagination-issue/29515 "2008-05-19T04:37:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan\_Sharp](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@Dan\_Sharp](https://discuss.rubyonrails.org/u/Dan_Sharp)\
**Post date:** [May 19, 2008, 4:37am UTC](https://discuss.rubyonrails.org/t/complex-pagination-issue/29515/1 "2008-05-19T04:37:34Z")

</div>

I wonder if anyone can give me some direction.

I'm trying to figure out how to handle pagination when I have a very complex way of building up a collection of objects. I've used the old classic pagination before, and I've used will\_paginate, but I don't think either will work in my case.

Briefly, I have a Location model that defines a physical location. I then build up a list of @locations by a variety of "filters"... for example, a text-based search query plus a geolocation radial search, plus one or more category selections. Underneath the hood, these are each separate queries that build a list of location objects. I then merge the results into a single instance variable: @locations, which is the array of matched Location objects.

Now, I wanna paginate that.

I suppose one option might be for me to try and build the thing into a single, monster SQL query that I can then add some LIMIT fields to... but for now, let's assume that's not an option. (I shiver at the complexity of it. oy!)

So... is there anything I can do with an existing set? Can will\_paginate support that? (I'm thinking no, but I figured I'd ask). I think the classic\_pagination plugin can, but I'm not really sure how.

Help?

-Danimal

---

<div class="post-metadata">

**Author:** ![Dan\_Sharp](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@Dan\_Sharp](https://discuss.rubyonrails.org/u/Dan_Sharp)\
**Post date:** [May 19, 2008, 5:05am UTC](https://discuss.rubyonrails.org/t/complex-pagination-issue/29515/2 "2008-05-19T05:05:52Z")

</div>

Never mind.

I was gonna just delete this post as I figured it out, but I'll leave it and post my solution in case it helps anyone else.

Turns out will\_paginate can handle this. You just wrap your Array with a WillPaginate::Collection and split it manually into the "windows" needed/desired for pagination.

Here's my code snippit. I start with @locations being the full array of matched locations. Then...

[code] &nbsp;&nbsp;page = (params[:page] ||= 1).to\_i &nbsp;&nbsp;PAGE\_SIZE = 10 # could be defined somewhere global &nbsp;&nbsp;offset = (page - 1) \* PAGE\_SIZE &nbsp;&nbsp;@locations = WillPaginate::Collection.new(page, PAGE\_SIZE, "#{@locations.length}").concat(@locations[offset..(offset + PAGE\_SIZE - 1)]) [/code]

Then, I just use the normal: \<%= will\_paginate @locations %\> code in my view and voila! Instant pagination! Whee!

"Dang, that was easy." \<-- me, about so many things of the Rails persuasion.

-Danimal

---

<div class="post-metadata">

**Author:** ![radar](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/radar/32/604_2.png) [@radar](https://discuss.rubyonrails.org/u/radar)\
**Post date:** [May 19, 2008, 5:12am UTC](https://discuss.rubyonrails.org/t/complex-pagination-issue/29515/3 "2008-05-19T05:12:58Z")

</div>

Getting the final @locations variable looks complicated to me, is this something you had to code for the solution to work, or is this how you’ve been using will\_paginate from the beginning?

---

<div class="post-metadata">

**Author:** ![AndyV](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@AndyV](https://discuss.rubyonrails.org/u/AndyV)\
**Post date:** [May 19, 2008, 11:39am UTC](https://discuss.rubyonrails.org/t/complex-pagination-issue/29515/4 "2008-05-19T11:39:46Z")

</div>

If the @locations collection is that complicated to assemble you might also want to introduce some caching on the results. Unless I misunderstand what you're doing it appears that you are going to have to calculate that collection on every page request and then select the appropriate results to return.

---

<div class="post-metadata">

**Author:** ![Dan\_Sharp](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@Dan\_Sharp](https://discuss.rubyonrails.org/u/Dan_Sharp)\
**Post date:** [May 19, 2008, 2:49pm UTC](https://discuss.rubyonrails.org/t/complex-pagination-issue/29515/5 "2008-05-19T14:49:18Z")

</div>

It was coded that way because of the application needs. Ultimately, it's that I have a potentially large set of Location objects and the end-user can choose one or more methods of "whittling" that set down (search, geolocation, categories, etc). So I have mulitple, complex finds that AND or OR merge together for a final results set. And that final set is what I need to paginate.

It may be worth a refactor attempt, but I think I might just have to live with it as-is and just do as much performance enhancements as I can... i.e. like caching as Andy suggested.

-Danimal

---

<div class="post-metadata">

**Author:** ![Jeffrey](https://avatars.discourse-cdn.com/v4/letter/j/d6d6ee/32.png) [@Jeffrey](https://discuss.rubyonrails.org/u/Jeffrey)\
**Post date:** [May 22, 2008, 5:34pm UTC](https://discuss.rubyonrails.org/t/complex-pagination-issue/29515/6 "2008-05-22T17:34:45Z")

</div>

Quoting Danimal \<fightonfightwell@gmail.com\>:

> Never mind.
> 
> I was gonna just delete this post as I figured it out, but I'll leave it and post my solution in case it helps anyone else.
> 
> Turns out will\_paginate can handle this. You just wrap your Array with a WillPaginate::Collection and split it manually into the "windows" needed/desired for pagination.
> 
> Here's my code snippit. I start with @locations being the full array of matched locations. Then...
> 
> [code] &nbsp;&nbsp;page = (params[:page] ||= 1).to\_i &nbsp;&nbsp;PAGE\_SIZE = 10 # could be defined somewhere global &nbsp;&nbsp;offset = (page - 1) \* PAGE\_SIZE &nbsp;&nbsp;@locations = WillPaginate::Collection.new(page, PAGE\_SIZE, "#{@locations.length}").concat(@locations[offset..(offset + PAGE\_SIZE - 1)]) [/code]
> 
> Then, I just use the normal: \<%= will\_paginate @locations %\> code in my view and voila! Instant pagination! Whee!

Here is an even simpler way:

[code] &nbsp;&nbsp;&nbsp;&nbsp;@locations = WillPaginate::Collection.create(page, PAGE\_SIZE, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;locations.size) do |pager| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pager.replace(locations[pager.offset, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[pager.per\_page, locations.size].min]) &nbsp;&nbsp;&nbsp;&nbsp;end [/code]

locations is the full array of locations, @locations is the WillPaginate object.

HTH, &nbsp;&nbsp;Jeffrey

P.S. not tested or guaranteed to work. I copy and pasted it from working code with editing of variables to match your code.

---

<div class="post-metadata">

**Author:** ![Dan\_Sharp](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@Dan\_Sharp](https://discuss.rubyonrails.org/u/Dan_Sharp)\
**Post date:** [May 22, 2008, 11:54pm UTC](https://discuss.rubyonrails.org/t/complex-pagination-issue/29515/7 "2008-05-22T23:54:19Z")

</div>

Jeffrey,

Thanks for the refactor! I like the block style! I'll have to incorporate that. Nice & clean.

-Danimal
