Forum: Ruby on Rails rand() order and Pagination

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E98de78bb42013c488fe8aa9d77d2cb1?d=identicon&s=25 steve.odom (Guest)
on 2005-11-29 22:26
(Received via mailing list)
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
47fb897113ebddae565f2f4b9f45ca1d?d=identicon&s=25 Jonathan <zjll9@imail.etsu.edu> (Guest)
on 2005-11-29 23:33
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
7e5be8d0d2537a6f6f00aada5541881e?d=identicon&s=25 sross (Guest)
on 2005-11-29 23:34
(Received via mailing list)
One way to tackle this would be to select randomly into a temporary
table,
then paginate that table.

--another steve
E98de78bb42013c488fe8aa9d77d2cb1?d=identicon&s=25 steve.odom (Guest)
on 2005-11-30 01:07
(Received via mailing list)
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
E98de78bb42013c488fe8aa9d77d2cb1?d=identicon&s=25 steve.odom (Guest)
on 2005-12-01 15:56
(Received via mailing list)
This worked great. Thanks!
0bcc972918891ef358b0b3dfb4e7f18b?d=identicon&s=25 hammed (Guest)
on 2005-12-01 16:49
(Received via mailing list)
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
This topic is locked and can not be replied to.