I have a player model and a game model set up with a HABTM.
I am trying to keep track of statistics for a player based on a hits and misses, which the user marks when editing a game. When the game is done editing an email is sent to the other team to confirm the game. If it is confirmed the statistics should be updated on the site. Here's what I have:
def confirm_game @games = Game.find(:all) for game in @games if (found the right game through hashing) game.update_attribute(:confirmed, true) game.update_statistics flash[:notice] = "Thank you for validating the game." break end end end
Game model def update_statistics for team in self.teams for player in team.players player.update_statistics(self) end team.update_statistics School.find(team.school_id).update_statistics end end
Player Model def update_statistics(game) for player in @game.players self.update_attribute(:hit_percentage, (player.hit.to_f / (player.hit.to_f + player.misses.to_f)) * 100) unless player.misses == 0 self.update_attribute(:opp_percentage, player.points.to_f / (player.hit.to_f + player.misses.to_f)) unless player.misses == 0 end end end
def add_hit self.update_attribute(:hit, hit + 1) end
I don't think this is the correct way to represent each hit a player has per game. How do I set it up like that? Also how do I write a view to show the game with each player's hits/ misses?