I have a ruby on rails app that lets users create anonymous profiles with their ideal jobs. Upon approval, I want to create a method that pulls 5-10 random users (who also have approved profiles) and send them an email asking to give feedback on the just-approved profile. I can do the email sending fine, but I am not sure the best way to get the random users as I need to. I can query for the approved status easy enough, but the randomization in some sort of loop is where I am unsure the best way to proceed.
Asked
Active
Viewed 16 times
0
-
https://stackoverflow.com/q/2752231/438992 But how "best" depends on context. – Dave Newton Sep 22 '22 at 19:13
1 Answers
0
You can get 5 random users by
User.order(Arel.sql('RANDOM()')).limit(5) # Postgres
User.order(Arel.sql('RAND()')).limit(5) # MySQL

Remy Wang
- 666
- 6
- 26