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.
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.
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.
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:
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.