Getting Random Records In Rails

Posted By Weston Ganger

Need to get a random record? Fortunately databases have built-in functions for this.

# For PostgreSQL and SQLite
Model.order("RANDOM()").first

# For MySQL
Model.order("RAND()").first

Related External Links:

Article Topic:Software Development - Ruby / Rails

Date:August 31, 2015