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....
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....
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.