Skip to content

Instantly share code, notes, and snippets.

@terrafied
Created April 5, 2011 10:44
Show Gist options
  • Save terrafied/903413 to your computer and use it in GitHub Desktop.
Save terrafied/903413 to your computer and use it in GitHub Desktop.
Return random records from a Rails model
module Randomizer
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def some(num = 5)
find :all, :order => 'rand()', :offset => (count * rand).to_i, :limit => num
end
def one
find(:all, :order => 'rand()', :offset => (count * rand).to_i, :limit => 1)[0]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment