Skip to content

Instantly share code, notes, and snippets.

@seban
Created February 7, 2010 12:14
Show Gist options
  • Save seban/297400 to your computer and use it in GitHub Desktop.
Save seban/297400 to your computer and use it in GitHub Desktop.
module MongoMapper
module FindRandom
def self.included(document)
document.extend ClassMethods
end
module ClassMethods
# Fetch random document from MongoDb
def find_random(conditions = {})
options = {}
options[:conditions] = conditions
options[:skip] = rand(self.collection.count)
self.find(:first, options)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment