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

Date:August 31, 2015

Recommended Posts

Contact

604-309-8713
info@solidfoundationwebdev.com

Send Me A Message

I would love to get in touch and talk about your next project. Feel free to send a message and I will get back to you shortly.

Get Connected