Ruby Active Record Associations in Rails More Association Options Polymorphic Options

Natalie Tan
Natalie Tan
25,519 Points

Poloymorphic Options in Rails

I was watching this video on polymorphic options https://teamtreehouse.com/library/polymorphic-options

I felt like the model used to demonstrate this concept was questionable.

Whilst both movies & shows can have many_actors through a "production", actors can also act in many movies & shows, and technically do not "belong_to" a production.

In reality, shouldn't that be a many to many relationship?

1 Answer

Tyler Maxwell
Tyler Maxwell
37,389 Points

Hi Natalie,

I see what you mean. The scenario they used is a bit confusing. The only way to have a many-to-many relationship is via a join table. A join table is basically just a table of foreign keys for each member of the relationship. If there is no join table, it can't be many-to-many. I hope that helps. Happy coding.

Natalie Tan
Natalie Tan
25,519 Points

Yup, i understand that they didn't code it to be a many-to-many example in the video. I'm just not sure why they chose to use a polymorphic relationship in this example.

I just understood the real world relationship between actors <--> movies , actors <--> shows. to be something like that:

Show & Movie Single Table Inheritance through Production

Show < Production

Movie < Production

Production

has_many : roles

has_many :actors, through :roles

Role

belongs_to :actor

belongs_to :production

Actor

has_many : roles

has_many :productions, through :roles