Paginate through a randomized list of blog posts using will_paginate

I want to give users the ability to view their blog posts in random order.

I can not implement it like this:

@posts = Post.paginate :page => params[:page], :order => 'RANDOM()'

since the parameter :orderis called with every request, and therefore I risk repeating blog entries.

What is the best way to do this?

+5
source share
4 answers

RAND accepts a seed in MySQL:

RAND(N) 

From MySQL Docs :

RAND (), RAND (N)

v 0 <= v < 1,0. N , , . , , RAND (3), , .

.

SAME , RAND, , .

, .

+3

( ), , - .

, , .

, , .

+1

: order = > RANDOM() , @posts, , paginate, .

0

named scope Post, :

class Post < ActiveRecord::Base
  named_scope :random, :order => 'RANDOM()'
  .
  .
  .
end

PostsController :

@posts = Post.random.paginate :page => params[:page]
-2

All Articles