diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 776dec5..9ac94f0 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -4,7 +4,7 @@ class Tournament < ActiveRecord::Base has_many :matches, dependent: :destroy has_many :mats, dependent: :destroy attr_accessor :upcomingMatches, :unfinishedMatches - serialize :matchups_array, Array + serialize :matchups_array def unfinishedMatches @@ -12,7 +12,7 @@ class Tournament < ActiveRecord::Base def upcomingMatches if self.matchups_array? - return self.matchups_array + return matchupHashesToObjects(self.matchups_array) else return generateMatchups end @@ -32,8 +32,8 @@ class Tournament < ActiveRecord::Base @upcomingMatches = weight.generateMatchups(@matches) end @upcomingMatches = assignBouts(@upcomingMatches) - #self.matchups_array = @upcomingMatches - #self.save + self.matchups_array = matchupObjectsToHash(@upcomingMatches) + self.save return @upcomingMatches end @@ -42,6 +42,43 @@ class Tournament < ActiveRecord::Base @matches = @bouts.assignBouts(matches) return @matches end + + + def matchupObjectsToHash(matches) + array_of_hashes = [] + matches.each do |m| + @matchHash = Hash.new + @matchHash["w1"] = m.w1 + @matchHash["w2"] = m.w2 + @matchHash["round"] = m.round + @matchHash["weight_id"] = m.weight_id + @matchHash["boutNumber"] = m.boutNumber + @matchHash["w1_name"] = m.w1_name + @matchHash["w2_name"] = m.w2_name + @matchHash["bracket_position"] = m.bracket_position + @matchHash["bracket_position_number"] = m.bracket_position_number + array_of_hashes << @matchHash + end + return array_of_hashes + end + + def matchupHashesToObjects(matches) + array_of_objects = [] + matches.each do |m| + @match = Matchup.new + @match.w1 = m["w1"] + @match.w2 = m["w2"] + @match.round = m["round"] + @match.weight_id = m["weight_id"] + @match.boutNumber = m["boutNumber"] + @match.w1_name = m["w1_name"] + @match.w2_name = m["w2_name"] + @match.bracket_position = m["bracket_position"] + @match.bracket_position_number = m["bracket_position_number"] + array_of_objects << @match + end + return array_of_objects + end end