Different Order by clause on will_paginate pages?

Hi im trying to list random records on first page and from 2nd page to
onwards, i need to show records by created_at using will_paginate plugin
but i can only apply one of these condition at a time.

Im showing based on order clause e,g

Order(‘RAND()’) or Order(‘created_at Desc’)

And then applying pagination on it. I don’t know how to apply both of
them with Random on ist page and created at on the rest of the
pagination. Anybody have any idea?

On Oct 26, 10:25am, Hussam A. [email protected] wrote:

pagination. Anybody have any idea?

That sounds like two mutually contradictory aims. You could perhaps
have a single ‘random’ page and then a series of paginated by
created_at pages.
If you absolutely had to present the illusion of the first page being
a random selection, you could also fiddle with the page parameter - if
it’s 1 show a random selection, if it’s greater than one subtract 1
and pass that to will_paginate

Fred

On Oct 27, 2011, at 2:46 AM, Hussam A. wrote:

it’s 1 show a random selection, if it’s greater than one subtract 1
order by random
end

Is there any other possible approach of handling it or how can i make
the above code more efficient???

I think you’re missing one thing here. This precise construction means
that there is a significant chance that some items will never be seen at
all. If the first “page” is random, it’s not “random within the
parameters of what would otherwise land on page 1”, it’s “random across
the entire database”. And then you move to page 2, where it’s created_at
ber alles.

What I would do is have a separate route for “random tease of what’s in
the database”, and then have a real “everything, in order” route as is
normal for an index + pagination.

Walter

I think you’re missing one thing here. This precise construction means
that there is a significant chance that some items will never be seen at
all. If the first “page” is random, it’s not “random within the
parameters of what would otherwise land on page 1”, it’s “random across
the entire database”. And then you move to page 2, where it’s created_at
ber alles.

What I would do is have a separate route for “random tease of what’s in
the database”, and then have a real “everything, in order” route as is
normal for an index + pagination.

Thanks Walter, it would certainly miss some of the items, but what you
are saying to have separate route for random and for pagination is may
be possible but right now i do not understand how to implement this, but
i will try to do it and if u can elaborate it further with example then
it would be helpful for me.

Frederick C. wrote in post #1028581:

On Oct 26, 10:25am, Hussam A. [email protected] wrote:

pagination. Anybody have any idea?

That sounds like two mutually contradictory aims. You could perhaps
have a single ‘random’ page and then a series of paginated by
created_at pages.
If you absolutely had to present the illusion of the first page being
a random selection, you could also fiddle with the page parameter - if
it’s 1 show a random selection, if it’s greater than one subtract 1
and pass that to will_paginate

Fred

Ok i got your point, i have done this and it works,

if params[:page].to_i > 1
order by created_at
else
order by random
end

Is there any other possible approach of handling it or how can i make
the above code more efficient???

On Oct 27, 2011, at 10:02 AM, Hussam A. wrote:

Thanks Walter, it would certainly miss some of the items, but what you
are saying to have separate route for random and for pagination is may
be possible but right now i do not understand how to implement this, but
i will try to do it and if u can elaborate it further with example then
it would be helpful for me.

#foos_controller.rb

def sample
@foos = Foo.order(‘RAND()’).limit(20) #mysql
#@foos = Foo.order(‘RANDOM()’).limit(20) #sqlite
end

routes.rb

resources :foos do
collection do
get ‘sample’
end
end

#foos/sample.html.erb

Hey look at all the cool foos we have!

<%= render @foos %>

#foos/_foo.html.erb

<%= foo.name %>

<%= foo.rank %>

<%= foo.serial_number %>

And then your index.html.erb would be exactly the same as it currently
is, with the pagination and whatnot, and your index (controller) method
would also be the same, with no worries about making the pagination
smarter. Wherever you wanted your random sample to appear, you would
simply use the sample method. And your index would be a normal index
without any notion of randomness, so any possible foo could be shown or
found.

Walter

And then your index.html.erb would be exactly the same as it currently
is, with the pagination and whatnot, and your index (controller) method
would also be the same, with no worries about making the pagination
smarter. Wherever you wanted your random sample to appear, you would
simply use the sample method. And your index would be a normal index
without any notion of randomness, so any possible foo could be shown or
found.

Thanks Walter, i appreciate your efforts, i understand your point here,
but what you are saying is to have two separate things, my point is to
show random items with pagination, we can show random items as shown
above but we cannot accommodate this thing with our overall pagination,
e.g

def index
@foo = Foo.order(‘created_at Desc’).paginate :page => params[:page],
:per_page => @per_page
end

As u are saying this would remain the same but my point is that we have
to accommodate that random sample method in the first page rather than
to have it separate from pagination. As in the above index method how
can we pass sample in it just for first page?

On Oct 28, 2011, at 3:15 AM, Hussam A. wrote:

show random items with pagination, we can show random items as shown
to have it separate from pagination. As in the above index method how
can we pass sample in it just for first page?

I’m making a more fundamental point. If you randomize the sort when
making your first page, it may not contain some elements that would
ordinarily be found in the first page (when sorted deterministically).
So you look at those things, and then you move on to page 2, which may
contain elements you already saw on page 1 (remember, that was
randomized) along with the rest of the next page of sorted elements. And
so on. So at each page after 1, you have a fair chance of seeing a
repeat, and you also have a statistically high chance of never seeing
some of the elements that would have appeared on the first page, and
this chance goes up the larger your total population of elements
becomes. When you move to page 2, your sorting mechanism assumes you’ve
seen everything that would sort to page 1, and never shows it to you
again.

Walter