Rails

Getting Random Records In Rails

Posted by Weston Ganger on August 31, 2015

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:


Posted in Rails and Tagged with postgresql database mysql random 


Want me to help develop your next project or application?

Contact Us

Specializing in Ruby-on-Rails, Javascript, Multi-Platform Electron Desktop Apps and Hybrid Cordova Mobile Apps.

Recommended Posts