diff --git a/app/models/bet.rb b/app/models/bet.rb new file mode 100644 index 0000000..d9a736d --- /dev/null +++ b/app/models/bet.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class Bet < ApplicationRecord + belongs_to :user + belongs_to :match + belongs_to :team, optional: true +end diff --git a/app/models/match.rb b/app/models/match.rb index bc80977..7891276 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -5,7 +5,9 @@ class Match < ApplicationRecord belongs_to :stage, optional: true belongs_to :group, optional: true + has_many :match_scores, dependent: :destroy + has_many :bets, dependent: :destroy validates :match_scores, length: { maximum: 2 } diff --git a/app/models/team.rb b/app/models/team.rb index f266f9c..d81563e 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -4,6 +4,7 @@ class Team < ApplicationRecord belongs_to :tournament, optional: true has_many :group_scores, dependent: :destroy has_many :match_scores, dependent: :destroy + has_many :bets, dependent: :destroy validates :name, presence: true diff --git a/app/models/user.rb b/app/models/user.rb index a45995e..c21ab22 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -11,4 +11,5 @@ class User < ApplicationRecord validates :username, presence: true, uniqueness: { case_sensitive: false } has_many :tournaments, dependent: :destroy + has_many :bets, dependent: :destroy end diff --git a/db/migrate/0001_create_bets.rb b/db/migrate/0001_create_bets.rb new file mode 100644 index 0000000..08b83ea --- /dev/null +++ b/db/migrate/0001_create_bets.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class CreateBets < ActiveRecord::Migration[5.2] + def change + create_table :bets do |t| + t.references :user, index: true, null: false, foreign_key: { on_delete: :cascade } + t.references :match, index: true, null: false, foreign_key: { on_delete: :cascade } + t.references :team, index: true, null: true, foreign_key: { on_delete: :cascade } + + t.timestamps + end + end +end diff --git a/spec/models/bet_spec.rb b/spec/models/bet_spec.rb new file mode 100644 index 0000000..06e7a78 --- /dev/null +++ b/spec/models/bet_spec.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Bet, type: :model do + describe 'association' do + it { should belong_to :user } + it { should belong_to :match } + it { should belong_to(:team).optional } + end +end diff --git a/spec/models/match_spec.rb b/spec/models/match_spec.rb index 1491bfa..6610b96 100644 --- a/spec/models/match_spec.rb +++ b/spec/models/match_spec.rb @@ -5,6 +5,7 @@ require 'rails_helper' RSpec.describe Match, type: :model do context 'association' do it { should have_many :match_scores } + it { should have_many :bets } it { should belong_to(:stage).optional } it { should belong_to(:group).optional } end diff --git a/spec/models/team_spec.rb b/spec/models/team_spec.rb index c2f25e3..d6d097e 100644 --- a/spec/models/team_spec.rb +++ b/spec/models/team_spec.rb @@ -11,5 +11,6 @@ RSpec.describe Team, type: :model do it { should belong_to(:tournament).optional } it { should have_many :group_scores } it { should have_many :match_scores } + it { should have_many :bets } end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 7216b64..83ab130 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -5,6 +5,7 @@ require 'rails_helper' RSpec.describe User, type: :model do describe 'association' do it { should have_many :tournaments } + it { should have_many :bets } end describe 'validation' do