November 13, 2013
For one of our clients we need to display random records from the database.
That's easy enough. We can use random()
function.
Batch.published_and_featured.order('random()')
.paginate(per_page: 20, page: params[:page])
Here we are using PostgreSQL database but ,I believe, above query will also work on MySQL.
The problem here is that if the user clicks on next page then we will try to get next set of 20 random records. And since these records are truly random, sometimes the user might see the records which has already been seen in the first page.
The fix is to make it random but not truly random. It needs to be random with a seed.
In MySQL we can pass seed directly to random()
function.
Batch.published_and_featured.order('random(0.3)')
.paginate(per_page: 20, page: params[:page])
In PostgreSQL it is a little more cumbersome. We first need to set seed and then
the subsequent query's usage of random()
will make use of seed value.
Batch.connection.execute "SELECT setseed(0.2)"
Batch.published_and_featured.order('random()')
.paginate(per_page: 20, page: params[:page])
For different user we should use different seed value and this value should be
random. So we set the seed value in before_action
.
def set_seed
cookies[:random_seed] ||= SecureRandom.random_number
end
Now change the query to use the seed value and we are all set.
If this blog was helpful, check out our full blog archive.