Add teams method to group,match,stage

It returns the unique teams that compete within the object
This commit is contained in:
Daniel Schädler 2019-04-24 22:30:23 +02:00
parent 7eadc27c39
commit cb158fda06
3 changed files with 9 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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