I have a little web app where we need to paginate collections of items. I''m trying to sort the collection and then pass it, sorted, to a Paginator, and still be able to paginate through the collection, across several pages, with the new sort order. The only thing I''m doing differently from the regular scaffolding approach is this: @order_pages = Paginator.new(self, @orders.length, 10, (params[:page] ||= 1).to_i) It doesn''t work. @orders is sorted when I hand it to Paginator. Am I skipping a step? Does Paginator only work on run-of-the-mill SQL calls? I can''t seem to figure this out. -- Giles Bowkett http://www.gilesgoatboy.org
I used Paginator in two steps with count_by_sql and find_by_sql. Here''s an excerpt of what works for me. @page = (params[:page] ||= 1).to_i @items_per_page = (params[:items_per_page] ||= 20).to_i count_sql_query = "SELECT COUNT(*) FROM activities [...fancy query]" @activities_count = Activity.count_by_sql(count_sql_query) @activity_pages = Paginator.new(self, @activities_count, @items_per_page, @page) select_sql_query = "SELECT activities [...fancy query] LIMIT #{@activity_pages.current.offset}, # {@activity_pages.items_per_page}" @activities = Activity.find_by_sql(select_sql_query) - dan -- Dan Kohn <mailto:dan@dankohn.com> <http://www.dankohn.com/> <tel:+1-415-233-1000> On Jul 15, 2006, at 5:45 PM, Giles Bowkett wrote:> I have a little web app where we need to paginate collections of > items. I''m trying to sort the collection and then pass it, sorted, to > a Paginator, and still be able to paginate through the collection, > across several pages, with the new sort order. The only thing I''m > doing differently from the regular scaffolding approach is this: > > @order_pages = Paginator.new(self, @orders.length, 10, (params[:page] > ||= 1).to_i) > > It doesn''t work. > > @orders is sorted when I hand it to Paginator. Am I skipping a step? > Does Paginator only work on run-of-the-mill SQL calls? I can''t seem to > figure this out. > > -- > Giles Bowkett > http://www.gilesgoatboy.org > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On Saturday, July 15, 2006, at 6:45 PM, Giles Bowkett wrote:>I have a little web app where we need to paginate collections of >items. I''m trying to sort the collection and then pass it, sorted, to >a Paginator, and still be able to paginate through the collection, >across several pages, with the new sort order. The only thing I''m >doing differently from the regular scaffolding approach is this: > >@order_pages = Paginator.new(self, @orders.length, 10, (params[:page] >||= 1).to_i) > >It doesn''t work. > >@orders is sorted when I hand it to Paginator. Am I skipping a step? >Does Paginator only work on run-of-the-mill SQL calls? I can''t seem to >figure this out. > >-- >Giles Bowkett >http://www.gilesgoatboy.org >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsAre you sure you need a custom paginator? The standard one is pretty flexible. You can pass it options that you would normally use in a find to get the output just the way you like it. _Kevin www.sciwerks.com -- Posted with http://DevLists.com. Sign up and save your mailbox.
yes, I''m sure I need a custom paginator. I need to paginate over a collection sorted outside of SQL. since a find uses SQL, and this sorting occurs after the find, and after the SQL, I am absolutely certain I need a custom paginator. or actually I am not **absolutely** certain, but I''m pretty sure. the code draws from three tables and needs to sort its result set by any one of the data returned, i.e., item.vendor.username item.product.description item.fulfillment_date item.aggregated_fulfillment_date_of_custom_options ...the last being not a property but a method which adds up the fulfillment dates of custom options (duh) and then tacks on fourteen days just to be safe. I am almost certain that passing that method into find() as an argument would be more challenging than just creating a custom paginator or, if necessary, writing my own. (but I could be wrong!) anyway!! I may have found what looks like a possible solution in a blog: http://www.bigbold.com/snippets/posts/show/389# it is, of course, clean, pretty, and simple. ^_^ hooray. :) -- Giles Bowkett http://www.gilesgoatboy.org On 16 Jul 2006 03:10:44 -0000, Kevin Olbrich <devlists-rubyonrails@devlists.com> wrote:> > On Saturday, July 15, 2006, at 6:45 PM, Giles Bowkett wrote: > >I have a little web app where we need to paginate collections of > >items. I''m trying to sort the collection and then pass it, sorted, to > >a Paginator, and still be able to paginate through the collection, > >across several pages, with the new sort order. The only thing I''m > >doing differently from the regular scaffolding approach is this: > > > >@order_pages = Paginator.new(self, @orders.length, 10, (params[:page] > >||= 1).to_i) > > > >It doesn''t work. > > > >@orders is sorted when I hand it to Paginator. Am I skipping a step? > >Does Paginator only work on run-of-the-mill SQL calls? I can''t seem to > >figure this out. > > > >-- > >Giles Bowkett > >http://www.gilesgoatboy.org > >_______________________________________________ > >Rails mailing list > >Rails@lists.rubyonrails.org > >http://lists.rubyonrails.org/mailman/listinfo/rails > > Are you sure you need a custom paginator? The standard one is pretty > flexible. You can pass it options that you would normally use in a > find to get the output just the way you like it. > > _Kevin > www.sciwerks.com > > -- > Posted with http://DevLists.com. Sign up and save your mailbox. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >