# ActiveRecord: Pointing to two records in one table from a re

**URL:** <https://www.ruby-forum.com/t/activerecord-pointing-to-two-records-in-one-table-from-a-re/91531>\
**Category:** Rails\
**Created:** [April 10, 2007, 3:35am UTC](https://www.ruby-forum.com/t/activerecord-pointing-to-two-records-in-one-table-from-a-re/91531 "2007-04-10T03:35:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrmarkham](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f9ae1b/32.png) [@mrmarkham](https://www.ruby-forum.com/u/mrmarkham)\
**Post date:** [April 10, 2007, 3:35am UTC](https://www.ruby-forum.com/t/activerecord-pointing-to-two-records-in-one-table-from-a-re/91531/1 "2007-04-10T03:35:18Z")

</div>

I’m relatively new to Rails (well, I got involved in it last fall, but  
got away from it) and am trying to figure out a reasonable way to  
model the following:

team

- id
- name

game

- id
- first\_team\_id
- frist\_team\_score
- second\_team\_id
- second\_team\_score

There will be lots of other information, but I was hoping to find an  
elegant way to point to two different rows in the team table (ideally  
allowing ActiveRecord to help me out) from one row in the game table.

Obviously  
team - has\_many :games  
game - has\_many :teams {:limit =\> ‘2’}

Gets me somewhat there, but I also need to know things like the scores  
for each time. That way I can figure out which team won to determine  
things like season records, etc.

It’s a very simplified model, but if someone has a good way to handle  
that scenario, I think the rest will fall in place.

Thanks!  
Andy

---

<div class="post-metadata">

**Author:** ![mrmarkham](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f9ae1b/32.png) [@mrmarkham](https://www.ruby-forum.com/u/mrmarkham)\
**Post date:** [April 10, 2007, 10:32am UTC](https://www.ruby-forum.com/t/activerecord-pointing-to-two-records-in-one-table-from-a-re/91531/2 "2007-04-10T10:32:51Z")

</div>

something like this:

class Team \< ActiveRecord::Base  
has\_many :games\_as\_home, :foreign\_key =\> “first\_team\_id”  
has\_many :games\_as\_guest, :foreign\_key =\> “second\_team\_id”  
end

class Game \< ActiveRecord::Base  
belongs\_to :home\_team, :foreign\_key =\> “first\_team\_id”  
belongs\_to :guest\_team, :foreign\_key =\> “second\_team\_id”  
end

Then you can e.g. do:

@team = @Team.find(params[:id]  
@home\_games = @team.games\_as\_home  
@guest\_games = @team.games\_as\_guest  
@no\_of\_games = @home\_games.count + @guest\_games.count  
@home\_games\_won = @home\_games.count :conditions =\> “first\_team\_score \>  
second\_team\_score”  
@guest\_games\_won = @guest\_games.count, :conditions =\>  
“first\_team\_score \< second\_team\_score”  
@tie\_games = home\_games.count :conditions =\> “first\_team\_score =  
second\_team\_score” + @guest\_games.count :conditions =\>  
“first\_team\_score = second\_team\_score”

you could encapsulate some of that stuf into a Model method too, if  
you need to do those calculations often throughout your app.
