# Association Help , 2 Teams playing a FootballGame

**URL:** <https://www.ruby-forum.com/t/association-help-2-teams-playing-a-footballgame/117332>\
**Category:** Rails\
**Created:** [October 16, 2007, 1:12am UTC](https://www.ruby-forum.com/t/association-help-2-teams-playing-a-footballgame/117332 "2007-10-16T01:12:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![macarthy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@macarthy](https://www.ruby-forum.com/u/macarthy)\
**Post date:** [October 16, 2007, 1:12am UTC](https://www.ruby-forum.com/t/association-help-2-teams-playing-a-footballgame/117332/1 "2007-10-16T01:12:35Z")

</div>

So I have the models

class FootballGame \< ActiveRecord::Base  
end

and

class Team \< ActiveRecord::Base  
end

I want to be able to do something like this

@t1 = Team.create(:name=\>“Michigan Wolverines football”)  
@t1.save  
@t2 = Team.create(:name=\>“Penn State Nittany Lions football”)  
@t2.save

@g = FootballGame.create(:hometeam =\> @t1, :awayteam =\> @t2)  
@g.save

my migrations:

create\_table :football\_games do |t|  
t.column :hometeam, :integer  
t.column :awayteam, :integer  
end

create\_table :teamsdo |t|  
t.column :name, :integer  
end

I’ve tried various has\_one declarations but I keep ending up with NULL  
s for hometeam and awayteam…

Anyone help me ??? Thanks

---

<div class="post-metadata">

**Author:** ![macarthy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@macarthy](https://www.ruby-forum.com/u/macarthy)\
**Post date:** [October 16, 2007, 1:19am UTC](https://www.ruby-forum.com/t/association-help-2-teams-playing-a-footballgame/117332/2 "2007-10-16T01:19:00Z")

</div>

On 10/15/07, macarthy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ve tried various has\_one declarations but I keep ending up with NULL  
> s for hometeam and awayteam…
> 
> Anyone help me ??? Thanks
> 
> >

That’s because when you have the foreign key in the FootballGames  
table, you need to use belongs\_to instead of has\_one.

The easiest way is to make the columns hometeam\_id and awayteam\_id,  
and the model becomes

class FootballGame \< ActiveRecord::Base  
belongs\_to :hometeam, :class\_name =\> “Team”  
belongs\_to :awayteam, :class\_name =\> “Team”  
end

If you want to keep your field names hometeam and awayteam in the db,  
then you need to add the :foreign\_key option to belongs\_to.

Pat

---

<div class="post-metadata">

**Author:** ![macarthy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@macarthy](https://www.ruby-forum.com/u/macarthy)\
**Post date:** [October 16, 2007, 1:24am UTC](https://www.ruby-forum.com/t/association-help-2-teams-playing-a-footballgame/117332/3 "2007-10-16T01:24:51Z")

</div>

I was thinking that was only solution but for some reason it seemed  
wrong. Guess it is the only baked in method though.

FootballGame belongs to hometeam  
FootballGame belongs to awayteam

hmmm…

Thanks Justin
