You could use custom SQL SELECT ... FROM my_table ORDER BY RAND() LIMIT (N)
He Fred, that's a slick idea. I was mulling over the best way to do this -- in the course of programming an online game, I needed to pick n contestants from a pool of arbitrary size, then narrow that to n/2 contestants, then n/4, finally ending up with just one.
Adding and indexing a random number beforehand sounds like a good way to go. Thanks for the thought!
Hi there — if you need exactly six random records, take a look at random-rails. It fetches random rows with much better performance than ORDER BY RANDOM() and plays nicely with ActiveRecord. Example: User.where(active: true).random(count: 6). It’s open source, so you can inspect or adapt it: GitHub: random-rails ![]()