Skip to content

Instantly share code, notes, and snippets.

@mikeymicrophone
Created August 12, 2018 14:12
Show Gist options
  • Save mikeymicrophone/0909633d8d7b1ec857377b0021574b69 to your computer and use it in GitHub Desktop.
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.
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