Created
February 13, 2013 22:03
-
-
Save pgeraghty/4948780 to your computer and use it in GitHub Desktop.
ActiveRecord::Base.sample hack for choosing a random model instance - useful for tests and seeding
This file contains hidden or 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 ActiveRecord | |
class Base | |
# Use like User.sample or User.pluck(:id).sample | |
def self.sample | |
order('RAND()').first rescue nil | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment