Created
August 12, 2018 14:12
-
-
Save mikeymicrophone/0909633d8d7b1ec857377b0021574b69 to your computer and use it in GitHub Desktop.
These methods allow you to probabilistically reuse data rather than making a fresh record every single time you fabricate.
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
def find_or_fabricate model, attrs = {} | |
model_klass = model.to_s.camelize.constantize | |
if model_klass.where(attrs).count == 0 || die_roll(model_klass.where(attrs).count % 5) | |
Fabricate model, attrs | |
else | |
model_klass.where(attrs).offset(rand(model_klass.where(attrs).count)).first | |
end | |
end | |
def die_roll faces = 6 | |
rand(faces) == 0 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment