Rails random ActiveRecord during pagination

I am trying to display a series of photos in random order, paginating them. The problem is that on every pagination page a random array is regenerated; therefore, the user can see the photos on page 2 that he has already seen on page 1. Lame. Here is my controller:

def index
  @photos = Photo.order('random()').paginate(:per_page => 12, :page => params[:page])
end

I have been thinking about this for a while and cannot come up with a reasonable solution. Is it easy to do? This seems like a pretty common feature, but I just need to drop the random thing. Thoughts?

0
source share
1 answer

You can introduce some ordering into randomness by specifying a seed. Using this, subsequent calls will give you the opportunity to re-order.

. , MySQL,

@photos = Photo.order('rand(0.5)').paginate(:per_page => 12, :page => params[:page])

0,5 - rand().

, Postgres setseed(). , . , , .

+1

All Articles