Heya, what is a good way to paginate search results? I created a search method for my models, which even accepts :limit and :offset like the find methods but I can''t think of a good solution for this chicken and egg problem: @modl_pages = Paginator.new self, modl_count, 20, @params[''page''] sql = modl_pages.current.to_sql @modls = Modl.search(:all, :query => @query, :limit => sql[0], :offset => sql[1]) how do I get the count for the Paginator.new without performing the search? Or rather how do I avoid doing the search twice? Putting the count in the session is the only solution I can think of right now, so that only for the first page the search has to performed twice. thanks, Florian
On 9/4/05, Florian Munz <surf-SFNkG+ShufSELgA04lAiVw@public.gmane.org> wrote:> Heya, > > what is a good way to paginate search results?def search nt = Note.search(params[:q]) @pages = Paginator.new self, nt.size, NOTES_CONFIG[''notes_per_page''], @params[:page] first = @pages.current.offset last = [first + NOTES_CONFIG[''notes_per_page''], nt.size].min @notes = nt[first...last] end
Stoyan Zhekov <stoyan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> def search > nt = Note.search(params[:q]) > @pages = Paginator.new self, nt.size, > NOTES_CONFIG[''notes_per_page''], @params[:page] > first = @pages.current.offset > last = [first + NOTES_CONFIG[''notes_per_page''], nt.size].min > @notes = nt[first...last] > endyeah, I''ve thought about that but didn''t like the idea of having the whole -- problably huge -- search result set in memory. There must be a better way ;-) Florian
On søn, 2005-09-04 at 19:55 +0200, Florian Munz wrote:> Stoyan Zhekov <stoyan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > def search > > nt = Note.search(params[:q]) > > @pages = Paginator.new self, nt.size, > > NOTES_CONFIG[''notes_per_page''], @params[:page] > > first = @pages.current.offset > > last = [first + NOTES_CONFIG[''notes_per_page''], nt.size].min > > @notes = nt[first...last] > > end > > yeah, I''ve thought about that but didn''t like the idea of having the > whole -- problably huge -- search result set in memory. > > There must be a better way ;-)Don''t know what db you''re using, but how about running a select count(*) on the original select query? i.e. select count(*) from (select a, b, c, d from table); Atleast I think that should be possible in Oracle. Regards, Henning Pedersen