如何在数据库中存储不同用户的信息?

时间:2021-03-20 12:41:34

My first post :)

我的第一个帖子:)

I just started learning Rails and have some doubts about database modeling. I am building a site for one soccer club and have many different user types. Each user can login to a site and have his own control panel. They also want that each user has separate part on site for registration. So, on my site I will have links "Register as a player" and "Register as a coach".

我刚开始学习Rails,对数据库建模有一些疑问。我正在为一个足球俱乐部建立一个站点,并且有许多不同的用户类型。每个用户都可以登录到一个站点并拥有自己的控制面板。他们还希望每个用户在网站上有单独的注册部分。所以,在我的网站上我会有链接“注册为球员”和“注册为教练”。

These would be my users on site and their attributes:

这些是我在网站上的用户及其属性:

SuperAdmin (id, username, firstName, lastName, password)

Admin (id, username, firstName, lastName, password, is_enabled)

Coach (id, username, firstName, lastName, password, is_enabled, coachRole, salary, contractStarted, contractEnds, vacationDaysLeft)

Player (id, username, firstName, lastName, password, is_enables, salary, contactStarted, contractEnds, birthDate, height, weight, goals, assists, minutesPlayed, gamesPlayed)

What would be my options here?

我的选择是什么?

OPTION 1

选项1

I was thinking about doing something like this:

我想做这样的事情:

create_table "users", force: true do |t|
    t.string   "username"
    t.string   "userRole"
    t.boolean  "is_enabled"
    t.string   "firstName"
    t.string   "lastName"
    t.string   "password"
    t.string   "coachRole"
    t.integer  "salary"
    t.datetime "contractStarted"
    t.datetime "contractEnds"
    t.integer  "vacationDaysLeft"
    t.datetime "birthDate"
    t.integer  "height"
    t.integer  "weight"
    t.integer  "goals"
    t.integer  "assists"
    t.integer  "minutesPlayed"
    t.integer  "gamesPlayed"
    t.datetime "created_at"
    t.datetime "updated_at"
end

Column userRole would have values SuperAdmin, Admin, Coach, Player. Depending on that role I can grant access to certain pages to those users. The problem is that this table looks huge and would have many null values.

列userRole将具有值SuperAdmin、Admin、Coach、Player。根据这个角色,我可以向这些用户授予访问某些页面的权限。问题是这个表看起来很大,并且会有很多空值。

For example, if I create Coach, row in table will have the following values:

例如,如果我创建Coach,表中的行将具有以下值:

Coach["username", "Coach", "is_enabled", "firstName", "lastName", "password", "coachRole", "salary", "contractStarted", "contractEnds", "vacationDaysLeft", null, null, null, null, null, null, null, "created_at", "updated_at"]

Coach["username", "Coach", "is_enabled", "firstName", "lastName", "password", "coachRole", "salary", "contractStarted", " contractend ", "vacationDaysLeft",空,空,空,空,空,空,空,空,空,空,空,"created_at", "updated_at"]

If I create Admin, he will have these values:

如果我创建Admin,他将具有以下值:

Admin["username", "Admin", "is_enabled", "firstName", "lastName", "password", null, null, null, null, null, null, null, null, null, null, null, null, "created_at", "updated_at"]

Admin["username", "Admin", "is_enabled", "firstName", "lastName", "password", null, null, null, null, null, null, null, null, null, null, null, null, created_at" updated_at"]

So, only player would really have some use of this approach.

所以,只有玩家才会使用这种方法。

OPTION 2

选项2

Second I was thinking to do was to create one 'basic' model User which will hold common information:

其次,我想要做的是创建一个“基本”的模型用户,它将包含共同的信息:

create_table "users", force: true do |t|
    t.string   "username"
    t.string   "userRole"
    t.boolean  "is_enabled"
    t.string   "firstName"
    t.string   "lastName"
    t.string   "password"
    t.string   "type"
end

And then for all other models to create new models:

然后为所有其他模型创建新的模型:

class SuperAdmin < User
end

class Admin < User
end

class Coach < User
end

class Player < User
end

But then I've read that I would have the same problem because all those columns would again be in the same row for each user. Now I am really not sure how to handle this situation. I don't need any additional gems for authentication and authorization. I want to do it by myself to learn as much as possible.

但是我已经读过了,我将会遇到同样的问题,因为所有这些列将再次为每个用户都在相同的行中。现在我真的不知道该如何处理这种情况。我不需要任何额外的gem进行身份验证和授权。我想自己做,尽可能多地学习。

I really appreciate your time in helping me to handle this. Thank you.

我非常感谢你能抽出时间来帮我处理这件事。谢谢你!

1 个解决方案

#1


2  

One solution could be store the common part in table ”users“. And create a table "properties". The properties table have "key", "value" columns. You can store arbitrary properties in the table properties, like "coachRole", "salary" and other properties you may want to add in the future(very possible).

一种解决方案是将公共部分存储在表“users”中。并创建一个表“属性”。属性表有“键”、“值”列。您可以在表属性中存储任意属性,如“coachRole”、“salary”和您将来可能要添加的其他属性(非常可能)。

Models relationship could be:

模型关系可能是:

class User < ActiveRecord::Base 
  has_many :properties
end

class Porperty < ActiveRecord::Base 
  belongs_to :user
end

#1


2  

One solution could be store the common part in table ”users“. And create a table "properties". The properties table have "key", "value" columns. You can store arbitrary properties in the table properties, like "coachRole", "salary" and other properties you may want to add in the future(very possible).

一种解决方案是将公共部分存储在表“users”中。并创建一个表“属性”。属性表有“键”、“值”列。您可以在表属性中存储任意属性,如“coachRole”、“salary”和您将来可能要添加的其他属性(非常可能)。

Models relationship could be:

模型关系可能是:

class User < ActiveRecord::Base 
  has_many :properties
end

class Porperty < ActiveRecord::Base 
  belongs_to :user
end