Rails does not support this out of the box. I believe I achieved this with a model extension (I no longer use it because I force Postgresql), but something like this might work:
module Randomize extend ActiveSupport::Concern included do scope :random, -> { order(rand_cmd) } end module ClassMethods def rand_cmd if connection.adapter_name =~ /mysql/i 'rand()' else 'random()' end end end end
Then you can do
class Item include Randomize end Item.where(...).random.limit(...)
source share