# 'each' loop 2 times trough all records

**URL:** <https://www.ruby-forum.com/t/each-loop-2-times-trough-all-records/151739>\
**Category:** Rails\
**Created:** [October 24, 2008, 11:23pm UTC](https://www.ruby-forum.com/t/each-loop-2-times-trough-all-records/151739 "2008-10-24T23:23:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![thithi32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b782af/32.png) [@thithi32](https://www.ruby-forum.com/u/thithi32)\
**Post date:** [October 24, 2008, 11:23pm UTC](https://www.ruby-forum.com/t/each-loop-2-times-trough-all-records/151739/1 "2008-10-24T23:23:34Z")

</div>

Hello,

I’m having some troubles today with Ruby…

I do  
@league = League.find(params[:id], :include =\> [:teams, :games])

then  
@league.teams.each do |t|

and it goes 2 times through the teams list ??? even if  
@league.teams.size = 2.

Does anyone have a clue where it can come from??

Thank you for you help,

Thierry

---

<div class="post-metadata">

**Author:** ![thithi32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b782af/32.png) [@thithi32](https://www.ruby-forum.com/u/thithi32)\
**Post date:** [October 25, 2008, 12:37am UTC](https://www.ruby-forum.com/t/each-loop-2-times-trough-all-records/151739/2 "2008-10-25T00:37:15Z")

</div>

Sure there should be something about it because there’s 2 teams per  
games.  
But Teams belongs to League and Games belongs to 2 Teams. But since  
there a direct link between Team and League I didn’t expect the Games to  
affect the Team list (I would have expected it the other way around with  
2 times each games).

See below my models:

class League \< ActiveRecord::Base  
has\_many :teams  
has\_many :games, :through =\> :teams  
end

class Team \< ActiveRecord::Base  
belongs\_to :club  
belongs\_to :league  
has\_many :games, :foreign\_key =\> ‘team\_a\_id’  
has\_many :games, :foreign\_key =\> ‘team\_b\_id’  
end

class Game \< ActiveRecord::Base  
belongs\_to :team\_a, :class\_name =\> ‘Team’, :foreign\_key=\>‘team\_a\_id’  
belongs\_to :team\_b, :class\_name =\> ‘Team’, :foreign\_key=\>‘team\_b\_id’  
end

---

<div class="post-metadata">

**Author:** ![thithi32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b782af/32.png) [@thithi32](https://www.ruby-forum.com/u/thithi32)\
**Post date:** [October 25, 2008, 12:23am UTC](https://www.ruby-forum.com/t/each-loop-2-times-trough-all-records/151739/3 "2008-10-25T00:23:24Z")

</div>

Could the inclusion of the games records be multiplying the number of  
teams that get returned maybe? Any difference if you take :games out of  
that :include array there?

---

<div class="post-metadata">

**Author:** ![thithi32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b782af/32.png) [@thithi32](https://www.ruby-forum.com/u/thithi32)\
**Post date:** [October 25, 2008, 1:15am UTC](https://www.ruby-forum.com/t/each-loop-2-times-trough-all-records/151739/4 "2008-10-25T01:15:27Z")

</div>

Huh–interesting, those two has\_many :games calls in your Team class  
there. Does that result in a single @my\_team.games collection property  
that gives the complete list of games, whether they were team\_a or  
team\_b? My intuition is that the second call would sort of overwrite  
the first & you wouldn’t see the games from the team\_a\_id link.

I tried to test this design out (I’m using rails 2.0.2) and that seems  
to be what happens. Only one of the two teams I added to a game had  
anything in its .games collection.

I’d be tempted to do something like this instead:

class Team \< ActiveRecord::Base  
belongs\_to :club  
belongs\_to :league  
has\_many :a\_games, :class\_name =\> ‘game’, :foreign\_key =\>  
‘team\_a\_id’  
has\_many :b\_games, :class\_name =\> ‘game’, :foreign\_key =\>  
‘team\_b\_id’

def games  
# mash the two games collections together manually.  
a\_games + b\_games  
end

end

Not that any of this addresses your problem, necessarily…

-Roy

---

<div class="post-metadata">

**Author:** ![thithi32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b782af/32.png) [@thithi32](https://www.ruby-forum.com/u/thithi32)\
**Post date:** [October 25, 2008, 3:35am UTC](https://www.ruby-forum.com/t/each-loop-2-times-trough-all-records/151739/5 "2008-10-25T03:35:35Z")

</div>

Wow! Thank you for the help.

I came to the same conclusions as you that just the B team games  
counted…

I didn’t know I do this “def games” definition … I was looking for  
that 3 months ago! Thank you.

I used the .uniq function to clean multiple occurence of teams and  
games.

There probably is a better way to write the code below:  
@games = []  
listForTeams = @league.teams.uniq.dup  
listForTeams.each do |t|  
@games.concat(t.games)  
end  
@games.uniq!

I I has to change part of my code to deal with team.games as a function  
and not an association.  
For example, a replaced:

h[:gameplayed] = @games.find(:all, :conditions =\> [“team\_a\_id = (?) OR  
team\_b\_id = (?)”, t.id, t.id]).size()

by

gamesplayed = @games.select { |v| v.team\_a\_id == t.id || v.team\_b\_id ==  
t.id }  
h[:gamesplayed] = gamesplayed.size()

it works fine, I hope I’ll have no problem going to an Ajax table with  
those data.

Thanks again for your time.

Thierry
