Re: Random Records in Rails

Refactoring of: Random Records in Rails

Language: Ruby

class Widget < ActiveRecord::Base
 
  # ...
  # Only for mysql
  # RANDOM() will work for sqlite
  def self.random
    find(:first, :order => "RAND()", :limit => 1)
  end
 
end

# and then...

class SomeController < ApplicationController
 
  # ...
 
  def some_action
    @widget = Widget.random
  end
 
end
Reveal More
Added about 1 year ago by Twitterprofilephoto_normal maranh