# Simple active record modeling question

**URL:** <https://www.ruby-forum.com/t/simple-active-record-modeling-question/134540>\
**Category:** Rails\
**Created:** [April 11, 2008, 9:04pm UTC](https://www.ruby-forum.com/t/simple-active-record-modeling-question/134540 "2008-04-11T21:04:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 11, 2008, 9:04pm UTC](https://www.ruby-forum.com/t/simple-active-record-modeling-question/134540/1 "2008-04-11T21:04:09Z")

</div>

Let’s say that I’m trying to model a baseball Game, capturing the  
names of the home and away teams within the Game class.

Game has both a (has\_a) home\_team and an away\_team.

one could write the Game class like this:

class Game \< ActiveRecord::Base  
has\_a home\_team\_id  
has\_a away\_team\_id  
…  
end

but this necessitates 2 DB tables (home\_teams and away\_teams) with  
duplicated content.

is there a simple railsy way of capturing the fact that Game has both  
a home\_team and an away\_team but use only 1 table (called teams)? I  
realize space is cheap these days and i shouldn’t fret about  
duplication, but perhaps there’s a simpler way.

THX!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 11, 2008, 9:18pm UTC](https://www.ruby-forum.com/t/simple-active-record-modeling-question/134540/2 "2008-04-11T21:18:14Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Let’s say that I’m trying to model a baseball Game, capturing the  
> names of the home and away teams within the Game class.
> 
> Game has both a (has\_a) home\_team and an away\_team.
> 
> one could write the Game class like this:
> 
> class Game \< ActiveRecord::Base  
> has\_a home\_team\_id  
> has\_a away\_team\_id  
> …  
> end
> 
> but this necessitates 2 DB tables (home\_teams and away\_teams) with  
> duplicated content.
> 
> is there a simple railsy way of capturing the fact that Game has both  
> a home\_team and an away\_team but use only 1 table (called teams)? I  
> realize space is cheap these days and i shouldn’t fret about  
> duplication, but perhaps there’s a simpler way.
> 
> THX!

No. you need two foreign keys in games but only one teams model. I  
think that something like this is what you need, but this is untested.

class Teams

has\_many :home\_games, class =\> games, :foreign\_key =\>  
home\_team\_id  
has\_many :away\_games, class =\> games, :foreign\_key =\>  
away\_team\_id

class Games

belongs\_to :team, :foreign\_key =\> home\_team\_id  
belongs\_to :team, :foreign\_key =\> away\_team\_id

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 11, 2008, 9:22pm UTC](https://www.ruby-forum.com/t/simple-active-record-modeling-question/134540/3 "2008-04-11T21:22:20Z")

</div>

James B. wrote:

I said it was untested… snd this probably ahs errors as well. Check  
the ActiveRecord::Associations API for details.

> class Teams
> 
> has\_many :home\_games, :class =\> games, :foreign\_key =\> :home\_team\_id  
> has\_many :away\_games, :class =\> games, :foreign\_key =\> :away\_team\_id
> 
> class Games
> 
> belongs\_to :home\_team, :class =\> :teams, :foreign\_key =\> :home\_team\_id  
> belongs\_to :away\_team, :class =\> :teams, :foreign\_key =\> :away\_team\_id

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 11, 2008, 9:26pm UTC](https://www.ruby-forum.com/t/simple-active-record-modeling-question/134540/4 "2008-04-11T21:26:28Z")

</div>

Hi –

On Fri, 11 Apr 2008, James B. wrote:

> > has\_many :home\_games, :class =\> games, :foreign\_key =\> :home\_team\_id  
> > :class\_name =\> “Game”

> > has\_many :away\_games, :class =\> games, :foreign\_key =\> :away\_team\_id
> > 
> > class Games
> > 
> > belongs\_to :home\_team, :class =\> :teams, :foreign\_key =\> :home\_team\_id  
> > belongs\_to :away\_team, :class =\> :teams, :foreign\_key =\> :away\_team\_id

In Rails 2.0, the foreign key is derived from the association name, so  
all you need there is:

belongs\_to :home\_team, :class\_name =\> “Team”

and it will figure out that the foreign key is home\_team\_id (rather  
than team\_id).

David

–  
Rails training from David A. Black and Ruby Power and Light:  
ADVANCING WITH RAILS April 14-17 New York City  
INTRO TO RAILS June 9-12 Berlin  
ADVANCING WITH RAILS June 16-19 Berlin  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 11, 2008, 9:28pm UTC](https://www.ruby-forum.com/t/simple-active-record-modeling-question/134540/5 "2008-04-11T21:28:55Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

removed\_email\_address@domain.invalid wrote:  
|  
| Let’s say that I’m trying to model a baseball Game, capturing the  
| names of the home and away teams within the Game class.  
|  
| Game has both a (has\_a) home\_team and an away\_team.  
|  
| one could write the Game class like this:  
|  
| class Game \< ActiveRecord::Base  
| has\_a home\_team\_id  
| has\_a away\_team\_id  
| …  
| end  
|  
| but this necessitates 2 DB tables (home\_teams and away\_teams) with  
| duplicated content.  
|  
| is there a simple railsy way of capturing the fact that Game has both  
| a home\_team and an away\_team but use only 1 table (called teams)? I  
| realize space is cheap these days and i shouldn’t fret about  
| duplication, but perhaps there’s a simpler way.

You should fret about mormalizing your data, though. 😛

To wit:  
Table teams:  
id, team

Table games:  
id, home\_team\_id, away\_team\_id, score\_home\_team, score\_away\_team

########  
home\_team\_id = teams.id (foreign key constraint)  
away\_team = teams.id (foreign key constraint)

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)

~ I don’t need to compromise my principles, because they don’t have  
the  
slightest bearing on what happens to me anyway. – Calvin  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkf/u8oACgkQbtAgaoJTgL9rJQCffU1I1f1CaEMiQ9mlQYIs8gPo  
6zEAoKHOh7n+YJBdBUTaaMYJ7FfoMOWW  
=YoWf  
-----END PGP SIGNATURE-----
