Skip to content

Instantly share code, notes, and snippets.

@webzorg
Created March 11, 2019 09:19
Show Gist options
  • Save webzorg/15c72655186119b568ab3b50469234c8 to your computer and use it in GitHub Desktop.
Save webzorg/15c72655186119b568ab3b50469234c8 to your computer and use it in GitHub Desktop.
class Model < ApplicationRecord
has_many :tags
def generate_tag
tag = loop do
tag = (SecureRandom.random_number(9e9) + 1e9).to_i # 9 digit random int
break tag unless Tag.exists?(tag: tag)
end
tags.create(tag: tag)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment