# Sports Match Model

**URL:** <https://www.ruby-forum.com/t/sports-match-model/119003>\
**Category:** Rails\
**Created:** [November 2, 2007, 2:08am UTC](https://www.ruby-forum.com/t/sports-match-model/119003 "2007-11-02T02:08:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pootsy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecc23a/32.png) [@Pootsy](https://www.ruby-forum.com/u/Pootsy)\
**Post date:** [November 2, 2007, 2:08am UTC](https://www.ruby-forum.com/t/sports-match-model/119003/1 "2007-11-02T02:08:27Z")

</div>

Hi,

I’m looking to model a set of fixtures for a sports league.

I’ve got a team model {id\*, name}  
And a match model {id\*, date, homeTeam, homeScore, awayTeam,  
awayScore}

I’d like the team.id to appear in the match.homeTeam and  
match.awayTeam (ie. twice in the same record) as one team plays  
another in a given match. How would I go about modelling this in  
Rails?

I understand that in MySQL you select team.id twice giving each a  
different alias.

Thanks a lot,

Pootsy

---

<div class="post-metadata">

**Author:** ![Pootsy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecc23a/32.png) [@Pootsy](https://www.ruby-forum.com/u/Pootsy)\
**Post date:** [November 2, 2007, 2:35am UTC](https://www.ruby-forum.com/t/sports-match-model/119003/2 "2007-11-02T02:35:21Z")

</div>

On Nov 1, 2007, at 9:05 PM, Pootsy wrote:

> another in a given match. How would I go about modelling this in  
> Rails?
> 
> I understand that in MySQL you select team.id twice giving each a  
> different alias.
> 
> Thanks a lot,
> 
> Pootsy

class Match \< ActiveRecord::Base  
belongs\_to :home\_team, :class\_name =\> ‘Team’, :foreign\_key  
=\> :home\_team\_id  
belongs\_to :away\_team, :class\_name =\> ‘Team’, :foreign\_key  
=\> :away\_team\_id  
end

Note that the :foreign\_key is required in Rails 1.2 (the default being  
the class name with “\_id”), but will be redundant in Rails 2.0 as the  
default will be built from the association name.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
