For one of our clients we need to display random records from the database. That’s easy enough. We can use
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.
Fix in MySQL
In MySQL we can pass seed directly to
Fix in PostgreSQL
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.
Set seed value in before_action
For different user we should use different seed value and this value should be random. So we set the seed value in
Now change the query to use the seed value and we are all set.