如何在Rails中建立不同的用户 [英] How to model different users in Rails

查看:172
本文介绍了如何在Rails中建立不同的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个建立了授权和认证逻辑的用户模型。

I have a User model with authorisation and authentication logic built.

现在我意识到有三种不同类型的用户。我想要存储关于它们各自的不同信息。

Now I realise I have three different types of users. I want to store different information about each of them.

在Rails中处理这个问题的最好方法是什么?

What is the best way to handle this in Rails?

我已经看过STI,但是从我读过的看来,这是不恰当的,因为我会得到很多NULL我的数据库中的字段。

I've looked at STI but from what I've read feel it is inappropriate because I'll end up with a lot of NULL fields in my database.

理想情况下,我不想复制三种用户类型中的每一种的身份验证/授权逻辑。

Ideally I'd like to not duplicate the authentication / authorisation logic for each of the three user types.

每个用户在应用程序中也将具有不同的功能。

Each user will also have different functionality within the application.

推荐答案

您可以尝试使用多态关联,并创建表用户与数据所有类型的用户都将其他数据放在单独的表中。 Railscast epizode ,涵盖此主题。

You can try using polymorphic associations and creating table users with data that all types of users have and putting other data in seperate tables. Railscast epizode covering this topic.

这篇关于如何在Rails中建立不同的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆