Last active
August 29, 2015 13:57
-
-
Save dmitriy-sqrt/9690421 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UpdateTeamRatingsFromTeamScores < ActiveRecord::Migration | |
def up | |
rating = Rating.by_alias('starladder') | |
Team.all.each do |team| | |
prev_tscore = team.team_scores.first | |
current_team_rating = nil | |
team.team_scores.each_with_index do |tscore, index| | |
if (index == 0 && team.team_ratings.where(division_id: tscore.division_id).blank?) | |
#create new_team_rating with tscore | |
current_team_rating = new_team_rating | |
end | |
if (index == team.team_scores.size - 1 && team.team_ratings.where(division_id: tscore.division_id).blank?) | |
end | |
# #if tscore division changed | |
# #or its the last teamscore and we don`t have a team_rating for it yet | |
# if tscore.division_id != prev_tscore.division_id || | |
# (index == team.team_scores.size - 1 && team.team_ratings.where(division_id: tscore.division_id).count == 0) | |
# base_tscore = (tscore.division_id != prev_tscore.division_id) ? prev_tscore : tscore | |
# if (!team_rating = TeamRating.where(team_id: team.id, division_id: base_tscore.division_id).first) | |
# TeamRating.create( | |
# team_id: team.id, | |
# division_id: base_tscore.division_id, | |
# rating_id: rating.id, | |
# score: base_tscore.score, | |
# wins: base_tscore.wins, | |
# looses: base_tscore.looses, | |
# draws: base_tscore.draws, | |
# difference: base_tscore.difference | |
# ) | |
# else | |
# team_rating.update_attribute(:active, true) | |
# end | |
# end | |
prev_tscore = tscore | |
end | |
end | |
end | |
def down | |
end | |
endclass UpdateTeamRatingsFromTeamScores < ActiveRecord::Migration | |
def up | |
rating = Rating.by_alias('starladder') | |
Team.all.each do |team| | |
team_scores = team.team_scores.order(:created_at) | |
divisions = team_scores.collect(&:division_id).uniq | |
if divisions.size > 1 | |
divisions.each do |d| | |
unless team.team_ratings.any? {|tr| tr.division_id == d} | |
new_team_rating = TeamRating.create( | |
team_id: team.id, | |
division_id: d, | |
rating_id: rating.id, | |
active: true | |
) | |
#team_scores.select {|ts| ts.division_id = d}.map { |ts| ts.update_attribute :team_rating_id, new_team_rating.id } | |
team_scores.select {|ts| ts.division_id = d}.map { |ts| | |
TeamScore.connection.update("update team_scores SET team_rating_id=#{new_team_rating.id} where id=#{ts.id}") | |
} | |
#TeamScore.connection.update("update team_scores SET team_rating_id=#{new_team_rating.id} where division_id=#{d}") | |
# "update team_scores where `id` = ?" | |
end | |
end | |
end | |
end | |
# prev_tscore = team.team_scores.first | |
# current_team_rating = nil | |
# team.team_scores.each_with_index do |tscore, index| | |
# if (index == 0) | |
# if (current_team_rating = team.team_ratings.where(division_id: tscore.division_id).first) | |
# #Use existing team_rating | |
# current_team_rating.update_attribute(:active, true) | |
# else | |
# #Create new team_rating and use it | |
# current_team_rating = TeamRating.create( | |
# team_id: team.id, | |
# division_id: tscore.division_id, | |
# rating_id: rating.id | |
# ) | |
# end | |
# end | |
# if tscore.division_id != prev_tscore.division_id | |
# if (current_team_rating = team.team_ratings.where(division_id: tscore.division_id).first) | |
# current_team_rating.update_attribute(:active, true) | |
# else | |
# #Create new team_rating and use it | |
# current_team_rating = TeamRating.create( | |
# team_id: team.id, | |
# division_id: tscore.division_id, | |
# rating_id: rating.id | |
# ) | |
# end | |
# end | |
# tscore.update_attribute(:team_rating_id, current_team_rating.id) | |
# prev_tscore = tscore | |
# end | |
# end | |
end | |
def down | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment