# Alphabetize pagination

**URL:** <https://www.ruby-forum.com/t/alphabetize-pagination/174406>\
**Category:** Rails\
**Created:** [August 26, 2009, 2:38pm UTC](https://www.ruby-forum.com/t/alphabetize-pagination/174406 "2009-08-26T14:38:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![p\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@p\_W](https://www.ruby-forum.com/u/p_W)\
**Post date:** [August 26, 2009, 2:38pm UTC](https://www.ruby-forum.com/t/alphabetize-pagination/174406/1 "2009-08-26T14:38:52Z")

</div>

**Note** I have not tried anything about my question, nor Googled  
anything about it yet, I was just wondering if this was possible, so  
if you wanna answer with lmgtfy, rtfm, or anything like that, please  
get it out of the way immediately.

I have a simple address book app that I want to sort by last name, and  
put each letter on a different page. Can the Paginate Gem be  
customized this much, that is, to provide pagination by, let’s say  
record.last\_name, and instead of page numbers, show A…Z instead?

---

<div class="post-metadata">

**Author:** ![p\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@p\_W](https://www.ruby-forum.com/u/p_W)\
**Post date:** [August 26, 2009, 6:48pm UTC](https://www.ruby-forum.com/t/alphabetize-pagination/174406/2 "2009-08-26T18:48:30Z")

</div>

Not that I know of, or have done… but I haven’t investigated the  
possibility.

As an alternative, you could put a series of letter links at the top of  
your table (or down a side; images or CSS styled so they look nice)  
which filter your records by that letter, and leave the will\_paginate  
gem as is to paginate the table body using all the filtered entries  
returned from the query.

---

<div class="post-metadata">

**Author:** ![p\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@p\_W](https://www.ruby-forum.com/u/p_W)\
**Post date:** [August 26, 2009, 8:33pm UTC](https://www.ruby-forum.com/t/alphabetize-pagination/174406/3 "2009-08-26T20:33:47Z")

</div>

This is for a glossary that I setup …

class Glossary  
def self.find\_glossaries(options = {})  
if options[:category].blank?  
paginate :per\_page =\> options[:per\_page], :page =\> options  
[:page],  
:conditions =\> [“term LIKE ?”, options[:letter]+"%"], :order  
=\> “term”  
else  
end  
end  
end

Then in the controller  
@letter = params[:letter].blank? ?  
@category.glossaries.collect.first.term.first.upcase : params[:letter]  
@glossaries = @category.glossaries.find\_glossaries(:letter =\>  
@letter, :page =\> params[:page])

There is more to this but the the important one is the model  
find\_glossaries

And then this view helper:

def letter\_options(category=nil)  
if category.nil?  
@letter\_options\_list = Glossary.all.collect!{|c|  
c.term.first.upcase }.uniq.sort!  
else  
@letter\_options\_list = category.glossaries.collect!{|c|  
c.term.first.upcase }.uniq.sort!  
end  
end

I hope this get you going…

---

<div class="post-metadata">

**Author:** ![p\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@p\_W](https://www.ruby-forum.com/u/p_W)\
**Post date:** [August 26, 2009, 9:56pm UTC](https://www.ruby-forum.com/t/alphabetize-pagination/174406/4 "2009-08-26T21:56:56Z")

</div>

And I also made it all work via ajax using this simple railscast:

> **[\#174 Pagination with AJAX - RailsCasts](http://railscasts.com/episodes/174-pagination-with-ajax)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

---

<div class="post-metadata">

**Author:** ![p\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@p\_W](https://www.ruby-forum.com/u/p_W)\
**Post date:** [August 26, 2009, 9:51pm UTC](https://www.ruby-forum.com/t/alphabetize-pagination/174406/5 "2009-08-26T21:51:40Z")

</div>

Here’s how I implemented something similar.

On the index page of my contacts, I list them alphabetically by  
default, paginated with 10 per page. At the top of this index page, I  
spit out the alphabet with each letter having it’s own link. Click  
any letter and I return all the contacts whose last name starts with  
that letter, paginated 10 per page of that letter. I also add some  
css styling to the letter they clicked to show which letter they’re  
on. Hope that helps.

[http://gist.github.com/175741](http://gist.github.com/175741)
