# Database modeling

**URL:** <https://www.ruby-forum.com/t/database-modeling/135083>\
**Category:** Rails\
**Created:** [April 17, 2008, 5:45pm UTC](https://www.ruby-forum.com/t/database-modeling/135083 "2008-04-17T17:45:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 17, 2008, 5:45pm UTC](https://www.ruby-forum.com/t/database-modeling/135083/1 "2008-04-17T17:45:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 18, 2008, 5:09pm UTC](https://www.ruby-forum.com/t/database-modeling/135083/2 "2008-04-18T17:09:59Z")

</div>

I am not sure that your present approach with respect to the data is  
sound. Generally, statistics are derived and updated from accumulated  
raw data. What might prove more beneficial in the long run is to simply  
store each player’s raw performance. So, assuming baseball as the sport,  
one could store this information in the GamePlayer model.

Something like this (NOT TESTED)

class Player

has\_many :player\_game\_stats, :class\_name =\> ‘GamePlayer’  
has\_many :player\_games, :class\_name =\> ‘GamePlayer’, :include  
=\> :game  
has\_many :games, :through =\> :player\_games

class Games

has\_many :game\_players, :include =\> :player  
has\_many :line\_ups, :class\_name =\> ‘GamePlayer’  
has\_many :players, :through =\> :game\_players

class GamePlayer

belongs\_to :player  
belongs\_to :game

with model attributes / db columns:  
player\_id  
game\_id  
position\_played  
innings\_played  
batting\_order  
at\_bats  
strike\_outs  
base\_on-errors  
walks  
singles  
doubles  
triples  
hr  
rbi  
… and so forth.

Then you simply derive your statistics from the detailed records in  
player\_games. This can be done at the DBMS level using stored procedures  
in many cases thus simplifying your application even further.
