# Count games

**URL:** <https://www.ruby-forum.com/t/count-games/133979>\
**Category:** Rails\
**Created:** [April 6, 2008, 12:16am UTC](https://www.ruby-forum.com/t/count-games/133979 "2008-04-06T00:16:14Z")\
**Posts on this page:** 3\
**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 6, 2008, 12:16am UTC](https://www.ruby-forum.com/t/count-games/133979/1 "2008-04-06T00:16:14Z")

</div>

I’m making a site that will display statistics. For my question I have  
two models, a School and a Team.

A team has an integer, games.

What is the best way to output for each school, the total number of  
games for all the school’s teams?

---

<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 6, 2008, 3:58am UTC](https://www.ruby-forum.com/t/count-games/133979/2 "2008-04-06T03:58:36Z")

</div>

edberner wrote:

> I’m making a site that will display statistics. For my question I have  
> two models, a School and a Team.
> 
> A team has an integer, games.
> 
> What is the best way to output for each school, the total number of  
> games for all the school’s teams?

If this is a database or model question, sufficient use of has\_many  
directives  
will enable a line like school.team.map{|t| [t.name, t.games.count] }

If it’s a view question, the answer is some combination if

 or tags, to display each school, team, and game count.

So nobody here can tell what the “best” way is. Try asking a more  
specific question!

–  
Phlip

---

<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 6, 2008, 9:06pm UTC](https://www.ruby-forum.com/t/count-games/133979/3 "2008-04-06T21:06:22Z")

</div>

This worked.  
for school in @schools  
for team in school.teams  
school.games += team.games  
end  
end
