From cb158fda0627a3ad48f91b93c2cc161852cb05c1 Mon Sep 17 00:00:00 2001 From: Malaber <32635600+Malaber@users.noreply.github.com> Date: Wed, 24 Apr 2019 22:30:23 +0200 Subject: [PATCH] Add teams method to group,match,stage It returns the unique teams that compete within the object --- app/models/group.rb | 4 ++++ app/models/match.rb | 4 ++++ app/models/stage.rb | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/models/group.rb b/app/models/group.rb index 39cba55..cc60392 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -4,4 +4,8 @@ class Group < ApplicationRecord belongs_to :stage has_many :matches, dependent: :destroy has_many :group_scores, dependent: :destroy + + def teams + matches.map{ |m| m.teams}.flatten.uniq + end end diff --git a/app/models/match.rb b/app/models/match.rb index 0eb0c14..c13eaf7 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -11,6 +11,10 @@ class Match < ApplicationRecord validate :stage_xor_group + def teams + match_scores.map{ |ms| ms.team}.flatten.uniq + end + private def stage_xor_group diff --git a/app/models/stage.rb b/app/models/stage.rb index 5e1d9d0..a00ab0e 100644 --- a/app/models/stage.rb +++ b/app/models/stage.rb @@ -6,6 +6,6 @@ class Stage < ApplicationRecord has_many :groups, dependent: :destroy def teams - groups.map{|g| g.matches.map{ |m| m.match_scores.map{ |ms| ms.team}}}.flatten.uniq + groups.map{|g| g.teams}.flatten.uniq end end