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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
skurmedel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Sep-12 21:53 UTC
Re: Paginating by initial letter (instead of N per page)
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. http://www.w3schools.com/sql/sql_where.asp --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Dan Tenenbaum
2006-Sep-12 22:16 UTC
Re: Paginating by initial letter (instead of N per page)
skurmedel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org 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. > > http://www.w3schools.com/sql/sql_where.aspUh, 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
skurmedel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Sep-12 22:39 UTC
Re: Paginating by initial letter (instead of N per page)
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---