module HasUniqueRandom::ModelAdditions

Public Instance Methods

has_unique_random(attribute, length: 8) click to toggle source
# File lib/has_unique_random/model_additions.rb, line 3
def has_unique_random(attribute, length: 8)
  before_create do
    begin
      id = HasUniqueRandom.generate_unique_random(length)
    end while self.class.exists?("#{attribute}" => id)

    send("#{attribute}=", id)
  end
end