# Help with model! Do I need to use a model as a join table?

**URL:** <https://www.ruby-forum.com/t/help-with-model-do-i-need-to-use-a-model-as-a-join-table/86174>\
**Category:** Rails\
**Created:** [February 20, 2007, 12:24pm UTC](https://www.ruby-forum.com/t/help-with-model-do-i-need-to-use-a-model-as-a-join-table/86174 "2007-02-20T12:24:18Z")\
**Posts on this page:** 5\
**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:** [February 20, 2007, 12:24pm UTC](https://www.ruby-forum.com/t/help-with-model-do-i-need-to-use-a-model-as-a-join-table/86174/1 "2007-02-20T12:24:18Z")

</div>

HI all, newbie question, I’m not sure exactly how I should model the  
following DB Structure, do I need to use a model as a join table? Here  
is my database

actors

- id
- name
- age

movie

- id
- name
- year

role

- id
- name
- requiresSwimming
- requiresHorseskills

movie\_actor

- actor\_id
- movie\_id
- role\_id

How should I map this in my model in rails? An actor can only have  
one role in each movie.

class Role \< ActiveRecord::Base  
end

class Movie \< ActiveRecord::Base  
end

class Actor \< ActiveRecord::Base  
end

Thanks J

---

<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:** [February 20, 2007, 12:30pm UTC](https://www.ruby-forum.com/t/help-with-model-do-i-need-to-use-a-model-as-a-join-table/86174/2 "2007-02-20T12:30:18Z")

</div>

Well, if each Role belongs to 1 specific movie and 1 actor, you don’t  
even need the movie\_actor table…  
us a has\_many :thorugh relationship, and roles as the join table…

actors

- id
- name
- age

movie

- id
- name
- year

role

- id
- movie\_id
- actor\_id
- name
- requiresSwimming
- requiresHorseskills

class Role \< ActiveRecord::Base  
belongs\_to :movie  
belongs\_to :actor  
end

class Movie \< ActiveRecord::Base  
has\_many :roles  
has\_many :actors, :through =\> :roles  
end

class Actor \< ActiveRecord::Base  
has\_many :roles  
has\_many :movies, :through =\> :roles  
end

@actor= Movie.find(1).roles[1].actor

# find the actor who plays the first role in the first movie

---

<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:** [February 20, 2007, 12:48pm UTC](https://www.ruby-forum.com/t/help-with-model-do-i-need-to-use-a-model-as-a-join-table/86174/3 "2007-02-20T12:48:20Z")

</div>

I can’t do that because the Roles are a standard set e.g.

Leading Man  
Leading Lady

so although Tom Cruise can only be Leading Man once on Mission  
Impossible, Tom can also be Leading man once on Mission Impossible 2

so I do need that actor\_roles table

So how should I map that ?

J

---

<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:** [February 20, 2007, 1:00pm UTC](https://www.ruby-forum.com/t/help-with-model-do-i-need-to-use-a-model-as-a-join-table/86174/4 "2007-02-20T13:00:21Z")

</div>

ah ok i get it. the attributes like “requires\_horseskills” made it  
look like indiviudal entries as opposed to a standard set.  
well then do this:

actors

- id
- name
- age

movie

- id
- name
- year

role

- id
- name
- requiresSwimming
- requiresHorseskills

Appearance

- id
- movie\_id
- actor\_id
- role\_id

class Role \< ActiveRecord::Base  
has\_many :appearances  
has\_many :movies, :through =\> :appearances  
has\_many :actors, :through =\> :appearances  
end

class Movie \< ActiveRecord::Base  
has\_many :appearances  
has\_many :actors, :through =\> :appearances  
has\_many :roles, :through =\> :appearances  
end

class Actor \< ActiveRecord::Base  
has\_many :appearances  
has\_many :movies, :through =\> :appearances  
has\_many :roles, :through =\> :appearances

end

class Appearance \< ActiveRecord::Base  
belongs\_to :movie  
belongs\_to :actor  
belongs\_to :role  
end

… so now the Model Appearance is the center of the 3 others, and  
each of those can find it’s corresponding items in the other 2  
throught the appeances join table.

---

<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:** [February 21, 2007, 11:35am UTC](https://www.ruby-forum.com/t/help-with-model-do-i-need-to-use-a-model-as-a-join-table/86174/5 "2007-02-21T11:35:19Z")

</div>

Thanks I’ll try that
