Paginating by initial letter (instead of N per page)

I have this collection:

@documents = Document.find :all, :order => ‘title’

And I want to paginate it so that the first page shows all the documents
whose title has the initial letter A, B, or C (regardless of how many
that is). And the second page shows documents whose title begins with D,
E, or F (and this might be a different number of items than on the first
page). And so on to X,Y, and Z.

I looked at creating a custom paginator object but it seems pretty
tightly bound to knowing the number of items per page, having the same
number on each page (except the last) and using offsets to calculate
which records to fetch.

It probably wouldn’t be too hard to roll my own with this, but I am
wondering if there is any way to leverage the Paginator to help.

Thanks

You can do this simply by using :condition (SQL Where Clause), you will
have to use WHERE title LIKE “a%” (that would get all rows with the
title starting with an a). A simple explanation can be found here.

[email protected] wrote:

You can do this simply by using :condition (SQL Where Clause), you will
have to use WHERE title LIKE “a%” (that would get all rows with the
title starting with an a). A simple explanation can be found here.

SQL WHERE Clause

Uh, where do I put this condition? In the Paginator object? And how do I
get the condition to change when the user paginates through the list?
Thanks

Hehe, I assume you use this (I havent used the default paginator
myself, wrote my own):

http://api.rubyonrails.com/classes/ActionController/Pagination.html#M000104

Using that it should be easy, and it should also be easy to change the
condition. Lets say you have the controller show_news:

def show_news:
@sort_char = params[:sort_char]
@news_pages, @news =
paginate :people, :order => ‘last_name, first_name’, :condition
=> “title LIKE ‘#{@sort_char}%’”
end

and in your view you would have both the “sort_char” available as well
as all the pages starting with “sort_char”. To pass the sort_char to
the view you could use:

link_to “A”, {:action => “show_news”, :sort_char => “a”}

Of course you should write some helper-function that would print out
the whole alphabet. If you look at
http://api.rubyonrails.com/classes/ActionController/Pagination.html
especially “Custom/Classic Pagination” you could surely work out away
to do it without using the paginate-helper.