Skip to content

Instantly share code, notes, and snippets.

@nottombrown
Created August 15, 2013 03:05
Show Gist options
  • Save nottombrown/6237854 to your computer and use it in GitHub Desktop.
Save nottombrown/6237854 to your computer and use it in GitHub Desktop.
Monkeypatch sample functionality into ActiveRecord
class ActiveRecord::Base
def self.sample(count=nil)
all_ids = (1..self.count).to_a
if count
ids = all_ids.sample(count)
self.where(id: ids)
else
id = all_ids.to_a.sample
self.find(id)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment