Created
April 5, 2011 10:44
-
-
Save terrafied/903413 to your computer and use it in GitHub Desktop.
Return random records from a Rails model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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