简体   繁体   English

设计和多用户模型

[英]Devise and multiple users models

I'm fairly new to Rails and trying to setup an app that uses several types of users: Teacher , Student , Admin (and likely more to come). 我是Rails新手,并尝试设置一个使用几种类型用户的应用程序: TeacherStudentAdmin (可能还会更多)。

The reason I want different models is because the attributes for the various users differ. 我想要不同模型的原因是因为各个用户的属性不同。 For example, Teacher have topics taught, a profession, and a diploma. 例如,“ Teacher具有讲授的主题,专业和文凭。 Whereas Student and Admin do not. StudentAdmin则没有。 Students can leave reviews of teachers, etc. 学生可以留下对老师等的评论。

I thought of having a general User model for the general information such as name , email , and other contact information and each type of user would inherit of it: 我想到了一个用于一般信息(例如姓名电子邮件和其他联系信息)的一般User模型,每种类型的用户都将继承该模型:

class Admin < User
end

class Teacher < User
end

class Student < User
end

And so on. 等等。

I'm using Devise for authentication and used STI for differentiating between user roles through a role field in the table. 我正在使用Devise进行身份验证,并使用STI通过表中的角色字段来区分用户角色。

The thing is, where and how can I tell my app to generate a current_user of the right class when the user logs in? 问题是,当用户登录时,我该在哪里以及如何告诉我的应用程序生成正确类的current_user And where should I store the additional info on each model (ex the profession of a teacher). 我应该在哪里存储每种型号的其他信息(例如教师的职业)。 In the users table? 在用户表中?

About the relationship with their objects, for example, a teacher has many topics he teaches. 例如,关于与对象之间的关系,老师有许多他教的话题。 How can I make sure only teachers have topics and not students? 如何确定只有老师有话题,没有学生?

I've looked into CanCan but was utterly confused. 我调查了CanCan,但完全感到困惑。

My advice is : DONT DO THAT ! 我的建议是:不要这样做! You are mixing very different concerns and will end up with the infamous God Object. 您正在混合非常不同的关注点,最终会遇到臭名昭著的上帝对象。

User is here only for authentication, you should not mix it with your domain models. 用户仅用于身份验证,您不应将其与域模型混合使用。 Teacher / Student / Admin etc should be separate models from User. 教师/学生/管理员等应与用户分开使用。 You can eventually create a People table and have Teacher Student and Admin inherit from it. 您最终可以创建一个People表,并让Teacher Student和Admin继承自该表。

class Student < Person

Or have a different table for each of them so there's no possible confusion (also someone might be both a Teacher and an Admin for example, separating stuff keeps your options open) 或为每个表设置不同的表,这样就不会造成混乱(例如,某人可能既是教师又是管理员,分开的内容会使您的选择保持打开状态)

But whatever you choose keep User out of it ! 但是,无论您选择什么,都不要让用户参与其中!

If you want to tie a User to Teacher / Student / Admin use associations and give each of them a user_id. 如果要将用户绑定到教师/学生/管理员,请使用关联,并为每个关联指定一个user_id。

class Student < ActiveRecord::Base
  belongs_to :user
end

Please have a look into this link. 请查看此链接。 Hope this may helpful to you:- 希望这对您有帮助:

http://funonrails.com/2011/12/multiple-resources-registrations-with/

Also, you can manage relationship with their objects by making model(table) as polymorphic like:- 另外,您可以通过使模型(表)成为多态的来管理它们与对象的关系,例如:

# Topic Model:
belongs_to :readable, :polymorphic => true

Also in topic model there are two more fields:-
readable_id and readable_type.

# Teacher Model:
has_many :topics, as: :readable

# Sturdent Model:
has_many :topics, as: :readable

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

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