[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

noob rails question

mbeattie@alumni.rutgers.edu

8/17/2006 2:30:00 PM

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

2 Answers

szymon.rozga

8/17/2006 8:48:00 PM

0

mtbeedee@gmail.com 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

mbeattie@alumni.rutgers.edu

8/18/2006 3:09:00 PM

0

Szymon Rozga wrote:
> mtbeedee@gmail.com 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

Dont you need :foreign_key in there somehow? How does that work?