2

I need to pick a random document out of my database and wanted to know a better strategy than the Ruby shuffle method that is less resource intensive for large databases.

Rick
  • 8,366
  • 8
  • 47
  • 76

1 Answers1

2

Add a "random" field and have the default value set by a proc that returns a random float. Whenever you need to pull a random document, just type Model.where(:random.gt => rand).first

johnnyx25
  • 696
  • 1
  • 4
  • 4