# Noob rails question

**URL:** <https://www.ruby-forum.com/t/noob-rails-question/65339>\
**Category:** Ruby\
**Created:** [August 17, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/noob-rails-question/65339 "2006-08-17T16:38:27Z")\
**Posts on this page:** 3\
**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:** [August 17, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/noob-rails-question/65339/1 "2006-08-17T16:38:27Z")

</div>

I am trying to set up an app where I track games between teams and the  
winners… So I have one “teams” table with info like the name and  
coach and whatever with one row per team.

I then have a “game” table with the home, away, and winners. But each  
of these fields is a “team” really. How can I set up the model so that  
game.home and game.away make sense and both refer to the same “team”  
object. I want to be able to do things like “if game.winner ==  
game.home”. Is that clear?

I will put the relavent pieces that I have been trying below so you can  
laugh at my noobness. (and understand what I have)

Game migration:  
t.column :home\_id, :integer  
t.column :away\_id, :integer  
t.column :winner\_id, :integer

Team migration:  
t.column :name, :string  
t.column :coach, :string

Game model: (I dont know what this should look like at all but this is  
my guess)  
has\_one :home  
has\_one :away  
has\_one :winner

Team model:  
has\_many :games

Can anyone help me understand how this should look and why?

Thanks

---

<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:** [August 17, 2006, 10:51pm UTC](https://www.ruby-forum.com/t/noob-rails-question/65339/2 "2006-08-17T22:51:23Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I am trying to set up an app where I track games between teams and the  
> winners… So I have one “teams” table with info like the name and  
> coach and whatever with one row per team.
> 
> I then have a “game” table with the home, away, and winners. But each  
> of these fields is a “team” really. How can I set up the model so that  
> game.home and game.away make sense and both refer to the same “team”  
> object. I want to be able to do things like “if game.winner ==  
> game.home”. Is that clear?

has\_one :home, :class\_name =\> “Team”  
has\_one :away, :class\_name =\> “Team”  
has\_one :winner, :class\_name =\> “Team”

This is off the top of my head, but I believe that this is documented  
under the has\_one method in the Rails API.

-Szymon

---

<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:** [August 18, 2006, 5:11pm UTC](https://www.ruby-forum.com/t/noob-rails-question/65339/3 "2006-08-18T17:11:45Z")

</div>

Szymon R. wrote:

> has\_one :home, :class\_name =\> “Team”  
> has\_one :away, :class\_name =\> “Team”  
> has\_one :winner, :class\_name =\> “Team”
> 
> This is off the top of my head, but I believe that this is documented  
> under the has\_one method in the Rails API.
> 
> -Szymon

Dont you need :foreign\_key in there somehow? How does that work?
