# Ror models relations - advice?

**URL:** <https://www.ruby-forum.com/t/ror-models-relations-advice/195550>\
**Category:** Rails\
**Created:** [September 24, 2010, 11:22pm UTC](https://www.ruby-forum.com/t/ror-models-relations-advice/195550 "2010-09-24T23:22:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ed655f/32.png) [@kay](https://www.ruby-forum.com/u/kay)\
**Post date:** [September 24, 2010, 11:22pm UTC](https://www.ruby-forum.com/t/ror-models-relations-advice/195550/1 "2010-09-24T23:22:50Z")

</div>

Hello guys,

pretty simple:

table games:  
team1\_id  
team2\_id

table teams:  
id  
designation

which relations should i use to access the teams designations from the  
game model ?

---

<div class="post-metadata">

**Author:** ![kay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ed655f/32.png) [@kay](https://www.ruby-forum.com/u/kay)\
**Post date:** [September 25, 2010, 12:57am UTC](https://www.ruby-forum.com/t/ror-models-relations-advice/195550/2 "2010-09-25T00:57:48Z")

</div>

class Game \< ActiveRecor::Base  
has\_many :teams

class Team \< ActiveRecor::Base  
belongs\_to :game

call designation of a game like this

first get a game from the DB

@game = Game.find(params[:id])

@designations = @game.teams.collect(&:designation)

---

<div class="post-metadata">

**Author:** ![kay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ed655f/32.png) [@kay](https://www.ruby-forum.com/u/kay)\
**Post date:** [September 25, 2010, 1:52am UTC](https://www.ruby-forum.com/t/ror-models-relations-advice/195550/3 "2010-09-25T01:52:17Z")

</div>

radhames brito wrote:

> class Game \< ActiveRecor::Base  
> has\_many :teams
> 
> class Team \< ActiveRecor::Base  
> belongs\_to :game
> 
> call designation of a game like this
> 
> first get a game from the DB
> 
> @game = Game.find(params[:id])
> 
> @designations = @game.teams.collect(&:designation)

tried and got error:

undefined method `teams’ for nil:NilClass

isnt row names team1\_id and team2\_id a problem to call directly ?

---

<div class="post-metadata">

**Author:** ![kay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ed655f/32.png) [@kay](https://www.ruby-forum.com/u/kay)\
**Post date:** [September 25, 2010, 3:29am UTC](https://www.ruby-forum.com/t/ror-models-relations-advice/195550/4 "2010-09-25T03:29:37Z")

</div>

Not sure if you noticed the typo. Its ActiveRecord::Base (the “d” was  
missing in the answerer’s post).

– Raja

Helder O. wrote:

> radhames brito wrote:
> 
> > class Game \< ActiveRecor::Base  
> > has\_many :teams
> > 
> > class Team \< ActiveRecor::Base  
> > belongs\_to :game
> > 
> > call designation of a game like this
> > 
> > first get a game from the DB
> > 
> > @game = Game.find(params[:id])
> > 
> > @designations = @game.teams.collect(&:designation)
> 
> tried and got error:
> 
> undefined method `teams’ for nil:NilClass
> 
> isnt row names team1\_id and team2\_id a problem to call directly ?

---

<div class="post-metadata">

**Author:** ![kay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ed655f/32.png) [@kay](https://www.ruby-forum.com/u/kay)\
**Post date:** [September 25, 2010, 9:53am UTC](https://www.ruby-forum.com/t/ror-models-relations-advice/195550/5 "2010-09-25T09:53:17Z")

</div>

On 24 September 2010 23:56, radhames brito [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Game \< ActiveRecor::Base  
> has\_many :teams
> 
> class Team \< ActiveRecor::Base  
> belongs\_to :game

That will only allow a team to be involved in one game, is that is  
what is required?

Colin

---

<div class="post-metadata">

**Author:** ![kay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ed655f/32.png) [@kay](https://www.ruby-forum.com/u/kay)\
**Post date:** [September 25, 2010, 1:29pm UTC](https://www.ruby-forum.com/t/ror-models-relations-advice/195550/6 "2010-09-25T13:29:18Z")

</div>

clanlaw wrote:

> On 24 September 2010 23:56, radhames brito [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > class Game \< ActiveRecor::Base  
> > has\_manyï¿½ :teams
> > 
> > class Team \< ActiveRecor::Base  
> > belongs\_to :game
> 
> That will only allow a team to be involved in one game, is that is  
> what is required?
> 
> Colin

brito yes i notices the typo.

colin, one game has two teams, team1\_id and team2\_id .

I wanted this the right way or else i make a helper method that returns  
me the teams for each game, that’s not a big problem guys

---

<div class="post-metadata">

**Author:** ![kay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ed655f/32.png) [@kay](https://www.ruby-forum.com/u/kay)\
**Post date:** [September 25, 2010, 1:41pm UTC](https://www.ruby-forum.com/t/ror-models-relations-advice/195550/7 "2010-09-25T13:41:33Z")

</div>

On 25 September 2010 12:29, Helder O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > what is required?
> > 
> > Colin
> 
> brito yes i notices the typo.
> 
> colin, one game has two teams, team1\_id and team2\_id .

Yes, my question was how many games can a team be involved with.  
Radhames’ suggestion would only allow each team to be involved in one  
game.

Colin

---

<div class="post-metadata">

**Author:** ![kay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ed655f/32.png) [@kay](https://www.ruby-forum.com/u/kay)\
**Post date:** [September 25, 2010, 4:16pm UTC](https://www.ruby-forum.com/t/ror-models-relations-advice/195550/8 "2010-09-25T16:16:04Z")

</div>

hum, well yes the relations is for a game to have many teams , but no  
for  
teams to be able to be involve in many game, is just that its not very  
clear  
what the requirements are and took this relation a a guide

table games:  
team1\_id  
team2\_id

is a game having the id of 2 teams specifically.

i see you want to have teams involve in many games also, it appears you  
want  
to save the designations per game, because if you keep the designation  
in  
the teams table when a team is designation is edited it will change  
designations for pass games, so the best way to do it is to have a has  
many  
through relation with a designation table linking to the game and team  
table.

class Game \< ActiveRecord::Base  
has\_many :designations  
has\_many :teams , :through =\> designations

class Team \< ActiveRecord::Base  
has\_many :designations  
has\_many :games , :through =\> designations

class designation \< ActiveRecord::Base  
belongs\_to :game  
belongs\_to :team

Designations are now per team and per game so if you update a game or a  
team, designations in the past wont be affected.  
if you want to save more details per team and per game , change the name  
of  
the designation table to something more general  
and add all the fields that will be specific to the game played by those  
teams, like if is baseball, you can put the score, the homeruns and so  
on.  
You could call the table statistics or something like that.

you can call a designation for a game with

first get a game from the DB

@game = Game.find(params[:id])

@designations = @game.designations

to see how this kind of relations are made you can watch this railscasts

> **[\#47 Two Many-to-Many - RailsCasts](http://railscasts.com/episodes/47-two-many-to-many)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.
