MySQL's RAND() in Order By Clause in Rails

Posted on December 06, 2007

I used to use a slightly more complicated method of getting random values out of the database, until a friend turned me on to MySQL’s “rand()” operator.

Usage in Rails:
class Product
  def self.get_random
    Product.find(:first, :order => 'RAND()')
  end
end

Will get a random product for you out of your database.