是否应该使用一对一关系、列或其他内容?

发布于 2024-10-09 07:11:44 字数 198 浏览 1 评论 0原文

虽然我的问题具体是在 Ruby on Rails 中,但我也一般性地问:我有一个用户表,并希望指定一些用户为学生,其他用户为教师。在这种情况下设计架构的最佳方法是什么?

编辑: 虽然我最初接受了 vonconrad 的答案——它符合最初的标准——但我必须根据 nhnb 的反馈重新打开并调整问题。

如果学生和/或教师需要额外的(独特的)属性该怎么办?

Though my problem is specifically in Ruby on Rails, I'm also asking generally: I have a users table and want to designate some users to be students and others to be teachers. What is the best way to design the schema in this case?

Edit:
Though I originally accepted vonconrad's answer--it fits the original criteria--I have to reopen and adjust the question based on feedback from nhnb.

What should be done if students and/or teachers require additional (unique) attributes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小糖芽 2024-10-16 07:11:44

这很大程度上取决于教师和学生之间有多少不同的属性,以及添加或删除新的独特属性的频率。如果情况是它们差异很大,您有两种选择:

  1. 制作两个模型,一个用于学生,一个用于教师,并使用 ruby​​ include 共享两个模型之间的任何逻辑。任何与用户关联的关联,您都可以使用多态关联 (has_many :user, :polymorphic => true)
  2. 将属性放在另一个表中。例如:您将拥有 users 表和 user_attributes 表。 users 将具有 id、user_type、username 等user_attributes 将具有 id、user_id、attribute_name、value

但是,如果两个用户之间的不同属性很少且非常稳定,那么您应该考虑使用 单表继承

祝你好运!

This greatly depends on how many the different attributes between the teacher and student there'll be as well as how often new unique attributes will be added or removed. If the case is that they will differ a lot, you have two choses:

  1. Make two models, one for Student and one for Teacher and use ruby include to share any logic between the two models. Any associations that associate with a user, you would use a polymorphic association (has_many :user, :polymorphic => true)
  2. Put the attributes in another table. For example: you'll have the users table and a user_attributes table. users would have id, user_type, username, etc. and user_attributes would have id, user_id, attribute_name, value.

If, however, your different attributes between the two users are few and pretty rock solid, you should just consider using Single Table Inheritance.

Good luck!

半山落雨半山空 2024-10-16 07:11:44

在本例中,我只需使用一个名为 teacher 的布尔列。如果为真,则用户是教师。如果是假的,那就是学生。无需创建另一个模型。

如果您想要两个以上角色(例如学生、教师、管理员、看门人(?)),您可以添加另一个名为 UserGroup 的模型。您还必须在用户表中创建一个名为 user_group_id 的列。在新模型中,每个角色都有一行。然后,您指定以下关系:

class User < ActiveRecord::Base
  belongs_to :user_group
end

class UserGroup < ActiveRecord::Base
  has_many :users
end

这将允许您将每个用户分配到特定组。

In this case, I'd simply go with a single boolean column called teacher. If true, the user is a teacher. If false, it's a student. No need to create another model.

If you want more than two roles (say, student, teacher, administrator, janitor(?)), you can add another model called UserGroup. You'll also have to create a column in the users table called user_group_id. In the new model, you have a single row for each of the roles. Then, you specify the following relationships:

class User < ActiveRecord::Base
  belongs_to :user_group
end

class UserGroup < ActiveRecord::Base
  has_many :users
end

This will allow you to assign each user to a specific group.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文