1

i have a rails table called users and would love to know the best way to get random records from the database so it would be fast as if getting it like the normal

@users = User.all

THANKS....

Kadismile
  • 210
  • 3
  • 19

1 Answers1

3

Try this:

# Get 5 random users
@users = User.order('RAND()').limit(5)

Note that the RAND() (MySQL) function is called RANDOM() in PostgreSQL and SQLite.

Alessandro Desantis
  • 14,098
  • 1
  • 26
  • 32