diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 3b1ab44..3c97ddb 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -1,14 +1,17 @@ class Tournament < ActiveRecord::Base has_many :schools, dependent: :destroy has_many :weights, dependent: :destroy - has_many :matches, dependent: :destroy - has_many :mats, dependent: :destroy + has_many :mats, dependent: :destroy has_many :wrestlers, through: :weights + def matches + @matches = Match.where(tournament_id: self.id) + end + def upcomingMatches - if self.matches.nil? - return self.matches + if matches.nil? + return matches else @matches = generateMatchups saveMatchups(@matches) @@ -17,6 +20,7 @@ class Tournament < ActiveRecord::Base end def generateMatchups + destroyAllMatches @matches = [] self.weights.map.sort_by{|x|[x.max]}.each do |weight| @matches = weight.generateMatchups(@matches) @@ -34,10 +38,17 @@ class Tournament < ActiveRecord::Base def saveMatchups(matches) matches.each do |m| + m.tournament_id = self.id m.save end end + def destroyAllMatches + matches.each do |m| + m.destroy + end + end + end diff --git a/app/models/weight.rb b/app/models/weight.rb index 81803d9..83a3754 100644 --- a/app/models/weight.rb +++ b/app/models/weight.rb @@ -5,8 +5,7 @@ class Weight < ActiveRecord::Base attr_accessor :pools before_save do - self.tournament.matchups_array = nil - self.tournament.save + self.tournament.destroyAllMatches end def pools diff --git a/app/models/wrestler.rb b/app/models/wrestler.rb index ab281f3..391b0da 100644 --- a/app/models/wrestler.rb +++ b/app/models/wrestler.rb @@ -5,8 +5,7 @@ class Wrestler < ActiveRecord::Base attr_accessor :poolNumber before_save do - self.tournament.matchups_array = nil - self.tournament.save + self.tournament.destroyAllMatches end diff --git a/db/schema.rb b/db/schema.rb index 58f92c9..4cf1be0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150426191006) do +ActiveRecord::Schema.define(version: 20150426195714) do create_table "matches", force: :cascade do |t| t.integer "w1" @@ -55,7 +55,6 @@ ActiveRecord::Schema.define(version: 20150426191006) do t.string "director_email" t.datetime "created_at" t.datetime "updated_at" - t.text "matchups_array" end create_table "users", force: :cascade do |t| diff --git a/test/fixtures/tournaments.yml b/test/fixtures/tournaments.yml index 040d3e4..24c6945 100644 --- a/test/fixtures/tournaments.yml +++ b/test/fixtures/tournaments.yml @@ -6,8 +6,7 @@ one: address: Some Place director: Jacob Cody Wimer director_email: jacob.wimer@gmail.com - matchups_array: - + diff --git a/test/integration/poolbracket_matchups_test.rb b/test/integration/poolbracket_matchups_test.rb index a15507c..8ec7601 100644 --- a/test/integration/poolbracket_matchups_test.rb +++ b/test/integration/poolbracket_matchups_test.rb @@ -42,7 +42,8 @@ class PoolbracketMatchupsTest < ActionDispatch::IntegrationTest end def checkForByeInPool(tournament) - @matchups = tournament.generateMatchups + tournament.upcomingMatches + @matchups = tournament.matches tournament.weights.each do |w| w.wrestlers.each do |wr| @round = 1