I am allowing users to find a random record from a list of posts. I define the random function in my model like so:
def self.random
if (c = count) != 0
find(:first, :offset => rand(c))
end
end
I would really like to limit the possibility of duplicates. There are not a large number of posts so I am not worried about performance. Is there a simple way to ensure that all posts are displayed in a random order before cycling through the same posts again?
Depending on what SQL storage engine you’re using, many implement a RAND() function, so you could do something like this:
To return all posts in a random order.