I''m stumped with how to combine pagination and a rand order_by from my model. Let''s say I am a teacher and have a table full of test questions. I want to return a query of 20 questions and use the paginator to return 1 or 5 or 10 per page. The catch is I want the 20 question to be random and in random order so they don''t keep getting the first 20 questions. On each paginated page, the paginator performs a new query using a limit X and an offset X to return different sequential results. If I use order_by rand() in my query then I will get a random result, but there is a chance I will get a duplicate question. Does anyone have any suggestions on how to return random results with pagination? Thanks, Steve _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
steve.odom wrote:> I''m stumped with how to combine pagination and a rand order_by from my > model. > > Let''s say I am a teacher and have a table full of test questions. I want > to > return a query of 20 questions and use the paginator to return 1 or 5 or > 10 > per page. The catch is I want the 20 question to be random and in random > order so they don''t keep getting the first 20 questions. > > On each paginated page, the paginator performs a new query using a limit > X > and an offset X to return different sequential results. If I use > order_by > rand() in my query then I will get a random result, but there is a > chance I > will get a duplicate question. > > Does anyone have any suggestions on how to return random results with > pagination? > > Thanks, > > SteveThis should do it: in application.rb: ------------------ def paginate_collection(collection, options = {}) default_options = {:per_page => 10, :page => 1} options = default_options.merge options pages = Paginator.new self, collection.size, options[:per_page], options[:page] first = pages.current.offset last = [first + options[:per_page], collection.size].min slice = collection[first...last] return [pages, slice] end in your controller: ------------------- @question_pages, @questions = paginate_collection Question.find(:all, :order => "rand()"), :page => @params[:page] --Jonathan -- Posted via http://www.ruby-forum.com/.
One way to tackle this would be to select randomly into a temporary table, then paginate that table. --another steve On 11/29/05 1:24 PM, "Steve Odom" <steve.odom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m stumped with how to combine pagination and a rand order_by from my model. > > Let''s say I am a teacher and have a table full of test questions. I want to > return a query of 20 questions and use the paginator to return 1 or 5 or 10 > per page. The catch is I want the 20 question to be random and in random order > so they don''t keep getting the first 20 questions. > > On each paginated page, the paginator performs a new query using a limit X and > an offset X to return different sequential results. If I use order_by rand() > in my query then I will get a random result, but there is a chance I will get > a duplicate question. > > Does anyone have any suggestions on how to return random results with > pagination? > > Thanks, > > Steve >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks to both of you for the suggestions. I''m going to have to work through Jonathan''s suggestion - I don''t quite grasp it at first glance. I''m using a temp table solution to do something else, so that approach is a possiblity. Steve On 11/29/05, Steve Ross <sross-ju+vs0qJmycyYsO2DCxJlVaTQe2KTcn/@public.gmane.org> wrote:> > One way to tackle this would be to select randomly into a temporary table, > then paginate that table. > > --another steve > > > On 11/29/05 1:24 PM, "Steve Odom" <steve.odom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I''m stumped with how to combine pagination and a rand order_by from my > model. > > Let''s say I am a teacher and have a table full of test questions. I want > to return a query of 20 questions and use the paginator to return 1 or 5 or > 10 per page. The catch is I want the 20 question to be random and in random > order so they don''t keep getting the first 20 questions. > > On each paginated page, the paginator performs a new query using a limit X > and an offset X to return different sequential results. If I use order_by > rand() in my query then I will get a random result, but there is a chance I > will get a duplicate question. > > Does anyone have any suggestions on how to return random results with > pagination? > > Thanks, > > Steve > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
This worked great. Thanks! On 11/29/05, Jonathan <zjll9-qOlZ2LGq26weF4Ui59n7hQ@public.gmane.org> <zjll9-qOlZ2LGq26weF4Ui59n7hQ@public.gmane.org> wrote:> > steve.odom wrote: > > I''m stumped with how to combine pagination and a rand order_by from my > > model. > > > > Let''s say I am a teacher and have a table full of test questions. I want > > to > > return a query of 20 questions and use the paginator to return 1 or 5 or > > 10 > > per page. The catch is I want the 20 question to be random and in random > > order so they don''t keep getting the first 20 questions. > > > > On each paginated page, the paginator performs a new query using a limit > > X > > and an offset X to return different sequential results. If I use > > order_by > > rand() in my query then I will get a random result, but there is a > > chance I > > will get a duplicate question. > > > > Does anyone have any suggestions on how to return random results with > > pagination? > > > > Thanks, > > > > Steve > > This should do it: > > in application.rb: > ------------------ > def paginate_collection(collection, options = {}) > default_options = {:per_page => 10, :page => 1} > options = default_options.merge options > > pages = Paginator.new self, collection.size, options[:per_page], > options[:page] > first = pages.current.offset > last = [first + options[:per_page], collection.size].min > slice = collection[first...last] > return [pages, slice] > end > > in your controller: > ------------------- > @question_pages, @questions = paginate_collection Question.find(:all, > :order => "rand()"), :page => @params[:page] > > --Jonathan > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Jonathan, Wouldn''t this give you a new randomized list on every page? ie. when you''re on page one, you get a the first set of 10 random questions, then on page 2 rand() is called again and you get another 10 random numbers which may include questions that were displayed on the first page. Am I missing something? Thanks Hammed On 29/11/05, Jonathan <zjll9-qOlZ2LGq26weF4Ui59n7hQ@public.gmane.org> <zjll9-qOlZ2LGq26weF4Ui59n7hQ@public.gmane.org> wrote:> > steve.odom wrote: > > I''m stumped with how to combine pagination and a rand order_by from my > > model. > > > > Let''s say I am a teacher and have a table full of test questions. I want > > to > > return a query of 20 questions and use the paginator to return 1 or 5 or > > 10 > > per page. The catch is I want the 20 question to be random and in random > > order so they don''t keep getting the first 20 questions. > > > > On each paginated page, the paginator performs a new query using a limit > > X > > and an offset X to return different sequential results. If I use > > order_by > > rand() in my query then I will get a random result, but there is a > > chance I > > will get a duplicate question. > > > > Does anyone have any suggestions on how to return random results with > > pagination? > > > > Thanks, > > > > Steve > > This should do it: > > in application.rb: > ------------------ > def paginate_collection(collection, options = {}) > default_options = {:per_page => 10, :page => 1} > options = default_options.merge options > > pages = Paginator.new self, collection.size, options[:per_page], > options[:page] > first = pages.current.offset > last = [first + options[:per_page], collection.size].min > slice = collection[first...last] > return [pages, slice] > end > > in your controller: > ------------------- > @question_pages, @questions = paginate_collection Question.find(:all, > :order => "rand()"), :page => @params[:page] > > --Jonathan > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails