From 20fafab009945e6a230f1306353061ed03acd7d8 Mon Sep 17 00:00:00 2001 From: Thor77 Date: Tue, 13 Nov 2018 16:53:28 +0100 Subject: [PATCH] Generate tournament code if not given --- app/models/tournament.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/models/tournament.rb b/app/models/tournament.rb index ff58cf7..d88b5ac 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -1,8 +1,15 @@ # frozen_string_literal: true +require 'securerandom' + class Tournament < ApplicationRecord validates :name, presence: true validates :code, presence: true, uniqueness: true + has_many :teams, dependent: :destroy belongs_to :user + + after_initialize do |tournament| + tournament.code ||= SecureRandom.hex 3 + end end