A day or two ago, a colleague pointed out to me that PostedJobs.ca was pretty thoroughly busted. After a moment of thinking, it occurred to me that I hadn’t done any pagination of the jobs lists, and over time they’d simply gotten too big to grab and display in one gulp. Oops. I mean, I knew it’d be a problem… I just didn’t expect it so soon.
After a little poking around and thinking, I figured that the built-in Rails pagination helpers weren’t going to be the best solution to this problem. At least not in the long run. It seems to be a thing amongst the Cool Kids to roll your own pagination whilst complaining about how the built-in pagination system doesn’t scale and such-like. Desiring to be one of the Cool Kids, and looking for something that would be more useful/flexible as the application grew, I set off in search of another solution.
I quickly came across the paginating_find plugin. It’s billed as “a plugin to help clean up and clarify pagination code in your Rails application.”
One of the things that immediately struck me about this solution was that it’s actually a replacement for, or more accurately an augmentation of, the #find method. Using the little extras the plugin gives you makes “stepping through data via your app’s UI, or loading large numbers of model instances little-by-little, to avoid consuming a ton of memory”, equally easy. Because it would be useful for more than just pagination, it seemed a good path to follow. After all, my problem wasn’t so much pagination as it was large result sets. Changing the behaviour of #find would help me deal with this problem wherever it occurred.
I did the standard script/plugin install and started banging away at our controller code.
Use of the plugin is pretty slick: you simply pass a :page option to the #find method. This means that “if you omit the :page option, the #find method operates as though the plugin is not installed.” Passing the :page option does two things: it counts the number of results in the result set; and “returns a special Enumerator that knows how to issue the find query, providing the correct LIMIT and OFFSET for any given logical page of model instances.” Dead simple, and wicked handy. It’s the Right Way to do it, and I didn’t have to code it myself. Bliss.
Tags: plugins · PostedJobs · Rails1 Comment


1 response so far ↓
I have found this plug-in very useful as well. However, during user acceptance testing, a user complained about the default paginating_links. There were 4 pages, and what showed up was
“1…4″ with the 1 not underlined and the 4 underlined (as a link). They thought that was confusing. I’ve looked into the plugin and wonder how to just get it to show 1 2 3 4 if there are only 4 pages, instead of the “…”, but haven’t found a way yet. Any ideas?