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 


Need help on your next project or application?

I specialize in Ruby-on-Rails, AngularJS, Javascript, Bootstrap, and Hybrid Mobile Apps with Cordova & Ionic.

Contact Me

Recommended Posts