Elliott Blatt
2006-Nov-24 18:42 UTC
How to override count_collection_for_pagination in rails?
Hi, My task seems simple, but apparently there is some gap in my understanding. Background: I''m working with an app that has a large number of records. (2.2 million). The problem is that these records need to be paged. The Rails paginiate method first does a "SELECT count(*) ..." to determine the size of the results set. Believe it or not, simply selecting the cound on 2.2 million records, takes about 1.4 seconds. Which is actually quite noticible to the user. My solution: So I''d like to work around this problem to by maintainng the count in a special table just for this purpose. Rails provides the module: "ActionController::Pagination" Which contains several Protected Instance methods On such method is: count_collection_for_pagination. The rails doc says: "Override this method to implement a custom counter." This is what I''ve tried to do to override the rails method: I put the following in my application_helper.rb module ActionController module Pagination module ProtectedInstanceMethods def count_collection_for_pagination(model, options) logger.debug(''this never gets called '') end end end end .......to no avail. The new method block does not get called. If anyone knows what I should be doing and/or why my solution is incorrect I''d really appreciate hearing. Thanks, EB -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Rob Sanheim
2006-Nov-24 21:42 UTC
Re: How to override count_collection_for_pagination in rails?
On 11/24/06, Elliott Blatt <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi, > > My task seems simple, but apparently there is some gap in my > understanding. > > Background: > > I''m working with an app that has a large number of records. (2.2 > million). The problem is that these records need to be paged. The Rails > paginiate method first does a "SELECT count(*) ..." to determine the > size of the results set. Believe it or not, simply selecting the cound > on 2.2 million records, takes about 1.4 seconds. Which is actually quite > noticible to the user.[snip] Stop right there. You are working with 2.2 million records, and you expecting built in pagination to work? Its not even recommended except for the simplest cases. Even if you get the method override working, you will have other issues down the road. I suggest you go custom or take a look at some of the plugins available for pagination. - rob -- http://www.robsanheim.com http://www.seekingalpha.com http://www.ajaxian.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?hl=en -~----------~----~----~----~------~----~------~--~---
Elliott Blatt
2006-Nov-24 23:29 UTC
Re: How to override count_collection_for_pagination in rails
Rob, Thanks for taking the time to answer my question. I appreciate the time and thought that you put into your answer. Having said that, I think I must disagree with you in a few places. The notion of paging 2.2 million records is not as ridiculous as it may seem; considering that these records can be sorted and filtered any number of ways. It''s quite common to for large result sets to be paged; one only need look at a google''s results to see this. It must be noted that rails does not fectch all 2.2 million and return the first page (10 say). It uses a SQL OFFSET and LIMIT to fetch only those records that are to be displayed on the the page. I don''t think you were implying this in your post, but many commenters on this subject seem to think that is what is happening; it is not. I did check out one of the paginators you alluded to, and it does seem adequate and I may try it. Not withstanding, I''d still be interested in investigation my larger theoretical question, which is, how do we over-ride Rails methods and why does not the example I gave in my original post work? Thanks again, Elliott Rob Sanheim wrote:> On 11/24/06, Elliott Blatt <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> paginiate method first does a "SELECT count(*) ..." to determine the >> size of the results set. Believe it or not, simply selecting the cound >> on 2.2 million records, takes about 1.4 seconds. Which is actually quite >> noticible to the user. > [snip] > > Stop right there. You are working with 2.2 million records, and you > expecting built in pagination to work? Its not even recommended > except for the simplest cases. Even if you get the method override > working, you will have other issues down the road. > > I suggest you go custom or take a look at some of the plugins > available for pagination. > > - rob > > -- > http://www.robsanheim.com > http://www.seekingalpha.com > http://www.ajaxian.com-- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Lionel Bouton
2006-Nov-24 23:42 UTC
Re: How to override count_collection_for_pagination in rails?
Elliott Blatt wrote the following on 24.11.2006 19:42 :> Hi, > > My task seems simple, but apparently there is some gap in my > understanding. > > Background: > > I''m working with an app that has a large number of records. (2.2 > million). The problem is that these records need to be paged. The Rails > paginiate method first does a "SELECT count(*) ..." to determine the > size of the results set. Believe it or not, simply selecting the cound > on 2.2 million records, takes about 1.4 seconds. Which is actually quite > noticible to the user. > > My solution: > > So I''d like to work around this problem to by maintainng the count in a > special table just for this purpose. > Rails provides the module: "ActionController::Pagination" > Which contains several Protected Instance methods > > On such method is: count_collection_for_pagination. > > The rails doc says: "Override this method to implement a custom > counter." > This is what I''ve tried to do to override the rails method: > I put the following in my application_helper.rb >This is an odd place to put such a thing. Helper methods are available in the view context (the corresponding modules are mixed in the views). I don''t know why the count_collection_for_pagination redefinition isn''t working here though (unless your code is in the ApplicationHelper module namespace instead of the root one). Just try to put the same code in your environment.rb (or one file it loads).> module ActionController > module Pagination > module ProtectedInstanceMethods > def count_collection_for_pagination(model, options) > logger.debug(''this never gets called '') > end > end > end > end > > > .......to no avail. The new method block does not get called. > > If anyone knows what I should be doing and/or why my solution is > incorrect I''d really appreciate hearing. > > Thanks, > EB > > > > >--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
Elliott Blatt
2006-Nov-25 01:21 UTC
Re: How to override count_collection_for_pagination in rails
Lionel Bouton wrote:> Elliott Blatt wrote the following on 24.11.2006 19:42 : >> size of the results set. Believe it or not, simply selecting the cound >> On such method is: count_collection_for_pagination. >> >> The rails doc says: "Override this method to implement a custom >> counter." >> This is what I''ve tried to do to override the rails method: >> I put the following in my application_helper.rb >> > > This is an odd place to put such a thing. Helper methods are available > in the view context (the corresponding modules are mixed in the views). > I don''t know why the count_collection_for_pagination redefinition isn''t > working here though (unless your code is in the ApplicationHelper module > namespace instead of the root one). > > Just try to put the same code in your environment.rb (or one file it > loads).Lionel, Thanks. The last time I over-rode an method, it was for the pagination_links helper. I thought the application helper would have broad enought scope. Meanwhile, I accomplished my task by using: http://codefluency.com/2006/10/24/paginator-released Thanks again, EB -- 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?hl=en -~----------~----~----~----~------~----~------~--~---