简体   繁体   English

Active Record DB 建模的正确方法

[英]Right way to Active Record DB modeling

If I use STI, I'll have several nil columns on my DB and I don't know if I can use Polymorphic Associations in cases like that.如果我使用 STI,我的数据库上将有几个 nil 列,我不知道在这种情况下是否可以使用多态关联。 Any tip or some references?任何提示或一些参考?

[!How the right way to modeling somethink like that?] [1][1] [!建模的正确方法是怎样思考的?] [1][1]

Though I don't understand the language completely from the image.虽然我从图像中完全看不懂语言。 If I understand I think one Coordinator has many Junior employee and senior employee.如果我明白,我认为一名协调员有许多初级员工和高级员工。 And seems like both employee table has the same type of column.并且似乎两个员工表都具有相同类型的列。 What I would do in this case is, I would just create one employee table and keep a enum column of role.在这种情况下,我会做的是,我只会创建一个员工表并保留一个角色枚举列。 The model would look something like below-该模型将如下所示 -

class Coordinator
 has_many: :employees
end

class Employee
 belongs_to: :coordinator

 enum role: { junior: 0, senior: 1 }
end

If you haven't used enum in Rails yet, it's really handy.如果你还没有在 Rails 中使用过 enum,它真的很方便。 Take a look看一看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM