I am trying to show some statistics for a dummy game.
There is a Team model, a Player model, and a Run model.
I can get runs for a specific month in the player model:
def count_runs(date)
self.runs.count(:conditions => {:created_at => (date.beginning_of_month..date.end_of_month)})
end
I can get them in the correct order in the controller and Team model:
@players = @team.players_by_count(Date.today)
def players_by_count(date)
@date = date
self.players.all.sort_by{|p| [-p.count_runs(@date)]}
end
I show that the table shows their position:
<table>
<% @players.each_with_index do |player, index| %>
<tr>
<td><%= (index+1).ordinalize %></td>
<td><%= player.name %></td>
<td><%= player.count_runs(Date.today) %></td>
</tr>
<% end %>
</table>
My diagram is as follows:
create_table "teams", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "players", :force => true do |t|
t.string "name"
t.integer "team_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "runs", :force => true do |t|
t.integer "player_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
I want to be able to determine what the average position is at the end of the month. Thus, at the end of each month they were in the positions (1st, 3rd, 1st, 5th / 4th month) = Average position = 2.5
I am also trying to figure out how I will get the winner (player placed on top) for each month.
Any ideas?