在我的 Rails 项目的 "has_many :through" 关系中,我应该使用 ENUM 还是创建另一个枢轴 table?

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

我是初学者 Rails 试图为电影数据库构建接口的开发人员。我正在使用名为 "persons_projects" 的枢轴 table 在人员模型和项目模型之间创建 "has_many :through" 多对多关系。此 table 当前有 2 列:person_id 和 project_id。我想添加另一列来指示此人在项目中扮演的角色(演员、导演等)。我应该为角色创建一个新的 table 并将新列作为 persons_projects 中的外键添加为 role_id 还是在此处我将使用具有诸如 [=17 之类的值的 ENUM =]、"DIRECTOR"?我从未使用过 ENUM,想知道这是否是一个好的用例?

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

不,你不应该,因为角色的数量就那么几个,stable。所以新建一个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?

当然,这绝对是使用enum的用例,你可以这样做:

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