Ensure uniqueness of generated Tournament.code

This commit is contained in:
Thor77 2018-12-02 13:51:54 +01:00
parent f9da696e3f
commit dc17cad154
1 changed files with 11 additions and 2 deletions

View File

@ -12,7 +12,16 @@ class Tournament < ApplicationRecord
alias_attribute :owner, :user
after_initialize do |tournament|
tournament.code ||= SecureRandom.hex 3
after_initialize :generate_code
private
def generate_code
return unless code.nil?
loop do
self.code = SecureRandom.hex(3)
break if errors['code'].blank?
end
end
end