soultrust
soultrust

Reputation: 599

Should I use an ENUM or create another pivot table in this "has_many :through" relationship in my Rails project?

I am beginner Rails developer trying to build an interface for a movie database. I am in the middle of creating a "has_many :through" many-to-many relationship between a person model and a project model using a pivot table called "persons_projects". This table currently has 2 columns: person_id and project_id. I want to add another column to indicate the role that the person is playing in the project (actor, director, etc). Should I create a new table for the roles and add the new column as a foreign key in persons_projects as role_id or is this where I would use an ENUM with values such as "ACTOR", "DIRECTOR"? I've never used ENUMs and was wondering if this is a good use case?

Upvotes: 0

Views: 394

Answers (1)

Hieu Pham
Hieu Pham

Reputation: 6692

Should I create a new table for the roles and add the new column as a foreign key in persons_projects as role_id?

No, you shouldn't, because the number of roles is just a few, and stable. So it is inefficient to create a new table.

I would use an ENUM with values such as "ACTOR", "DIRECTOR"? I've never used ENUMs and was wondering if this is a good use case?

Of course, this is absolutely the use case for using enum, you can do something like this:

class PersonProject < ActiveRecord::Base
  belongs_to :person
  belongs_to :project
  enum role: [:actor, :director]
end

Upvotes: 0

Related Questions