From d89c24a6454066934e1ddd9144187c273d392c9c Mon Sep 17 00:00:00 2001 From: Thor77 Date: Mon, 12 Nov 2018 15:10:52 +0100 Subject: [PATCH] Add relation between Tournament and User --- app/models/tournament.rb | 4 ++++ app/models/user.rb | 2 ++ db/migrate/0000_create_schema.rb | 7 +++++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 46e28f4..ff58cf7 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -1,4 +1,8 @@ # frozen_string_literal: true class Tournament < ApplicationRecord + validates :name, presence: true + validates :code, presence: true, uniqueness: true + + belongs_to :user end diff --git a/app/models/user.rb b/app/models/user.rb index ef5c4d4..b24614f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,4 +7,6 @@ class User < ApplicationRecord include DeviseTokenAuth::Concerns::User validates :username, presence: true, uniqueness: true + + has_many :tournaments, dependent: :destroy end diff --git a/db/migrate/0000_create_schema.rb b/db/migrate/0000_create_schema.rb index f8feccb..37821cf 100644 --- a/db/migrate/0000_create_schema.rb +++ b/db/migrate/0000_create_schema.rb @@ -40,11 +40,14 @@ class CreateSchema < ActiveRecord::Migration[5.2] end create_table :tournaments do |t| - t.string :name - t.string :code + t.string :name, null: false + t.string :code, null: false t.string :description t.boolean :public, default: true + # relation to owner + t.belongs_to :user, index: true, foreign_key: true, null: false + t.timestamps end