# Via: http://almosteffortless.com/2007/12/04/random-records-in-rails/ # REFACTORED TO USE MAX(ID) INSTEAD OF RETURNING ALL IDS class Widget < ActiveRecord::Base # ... def self.random # just get the max id, and use that as the seed for rand() max_id = connection.select_value("select max(id) from wigets") rand_id = rand(max_id) # find the first widget with an id equal to or greater than rand_id first(:conditions => "id >= #{rand_id}") || last end end # and then... class SomeController < ApplicationController def some_action @widget = Widget.random end end