Menu

Toshimaru's Blog

Get random records in Rails

MySQL

User.order("RAND()").limit(10)

PostgreSQL

User.order("RANDOM()").limit(10)

Ruby Array#sample

User.where(id: User.ids.sample(10))

Reference

Load more