Get a Random Model Record with ActiveRecord in Rails

In Rails 4+ with either Postgresql or SQLite by using RANDOM():

Model.order('RANDOM()').first

with MySQL:

Model.order('RAND()').first

Subscribe to My Newsletter

The latest programming-related news, articles and resources - sent to your inbox monthly. Unsubscribe anytime.