Home > Software design >  How best to query random user profiles
How best to query random user profiles

Time:09-23

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.

CodePudding user response:

You can get 5 random users by

User.order(Arel.sql('RANDOM()')).limit(5) # Postgres
User.order(Arel.sql('RAND()')).limit(5) # MySQL
  • Related