多对多,一对多

时间:2021-06-06 09:57:39

I need to set user permissions to edit other users profile.

我需要设置用户权限来编辑其他用户配置文件。

What I want is to be able to set for each user which user can be handled. So, let's say we have 5 users: A, B, C, D, E

我想要的是能够为每个用户设置可以处理的用户。假设有5个用户:A B C D E。

I want A to be able to handle B, C and D. I want B to be able to handle A, E, C. And so on...

我希望A能够处理B、C和d,我希望B能够处理A、E、C等等。

Is this a one-to-many or a many-to-many relation?

这是一对多关系还是多对多关系?

I'm not able to understand it because, it seems to me a one-to-many since there is one user having multiple users. But since the user itself can be handled by another one, makes me think it could be a many-to-many.

我无法理解,因为在我看来是一对多,因为有一个用户有多个用户。但是由于用户本身可以由另一个来处理,所以我认为它可以是多对多。

What questions should I ask to myself to identify the right relation?

我应该问自己哪些问题来确定正确的关系?

5 个解决方案

#1


5  

This is a many to many relation, as one account can edit many, and many accounts can edit one account.

这是一个多对多的关系,因为一个帐户可以编辑多个,许多帐户可以编辑一个帐户。

Example:

例子:

A can edit ABC  
B can edit AC 
C can edit BC

So A can be edited by multiple accounts (or belongs to multiple accounts), and can edit multiple accounts (or has multiple accounts)

A可以由多个账户(或属于多个账户)进行编辑,也可以编辑多个账户(或有多个账户)

#2


4  

Let's work through your problem and see how to identify the relationship.

让我们一起来解决你的问题,看看如何识别这种关系。

When you're writing the code that will check whether a user is authorized, you are going to have two main pieces of data: The user that is performing the edit and the user that is being edited. So, the question your application will be asking is "can user A edit user B?"

当您编写检查用户是否被授权的代码时,您将有两个主要的数据片段:执行编辑的用户和正在编辑的用户。因此,应用程序会问的问题是“用户A可以编辑用户B吗?”

Ignoring the semantics of relationships for a moment, think about how you'd look up the answer to that question:

忽略关系的语义,思考一下你是如何找到这个问题的答案的:

table can_edit:
  requestor_id | edited_id | permission
  =====================================
  User A       | User B    | YES
  User A       | User C    | YES
  User A       | User D    | YES
  User A       | User E    | NO
  User B       | User A    | YES
  User B       | User C    | YES
  User B       | User D    | NO
  etc...

Using this lookup table, you can determine who has access to edit whom. But, look again at what this table is describing. Since you're looking up the answer to a YES/NO question, you can express it more simply:

使用这个查找表,您可以确定谁有权编辑谁。但是,再看看这个表描述的是什么。既然你在寻找一个是或否问题的答案,你可以更简单地表达它:

table can_edit:
  requestor_id | edited_id | permission
  =====================================
  User A       | User B    | YES
  User A       | User C    | YES
  User A       | User D    | YES
  User B       | User A    | YES
  User B       | User C    | YES
  etc...

Here we've dropped the NO permissions. Now we can ask, "Do we have an entry in can_edit that matches our permission check?" If we perform a lookup and the row exists, then we can allow the access.

这里我们已经删除了NO权限。现在我们可以问,“can_edit中是否有与权限检查相匹配的条目?”如果执行查找,并且行存在,则可以允许访问。

Now, since the permission column will always be YES, it doesn't make much sense to include it. So now we have a table that is basically a list of user ID relationships.

既然权限列总是YES,那么包含它就没有多大意义了。现在我们有一个表基本上是用户ID关系的列表。

At this point we can draw out what the relationships look like using a database diagram:

此时,我们可以使用数据库图绘制出关系的样子:

users:
  user_id  <-----+
  email          |       can_edit:
  pass_digest    +-------- requestor_id
               +========== edited_id
users:         |
  user_id <====+
  email
  pass_digest

If we look at the example diagrams in the rails association guide, we can see that our diagram closely matches both has_many :through and has_and_belongs_to_many. These both describe many-to-many relationships.

如果我们查看rails关联指南中的示例图,就会发现我们的图与has_many:through和has_and_belongs_to_many非常匹配。它们都描述多对多关系。

As far as the implementation details go, you'll notice that both "can user A edit user B?" and "is user B a friend of user A?" are essentially the same question, just using different terms for the relationship. With that knowledge, you'll find that volumes have been written about how to define this self-referential has_many :through relationship, such as this excellent screencast by Ryan Bates.

就实现细节而言,您将注意到“用户A可以编辑用户B吗?”和“用户B是用户A的朋友吗?”这两个问题本质上是相同的,只是在关系中使用了不同的术语。有了这些知识,您将发现已经有很多关于如何定义这个自我引用的has_many:通过关系,例如Ryan Bates的出色的屏幕演示。

#3


2  

if you are not sure, i recommend that you read the guides about rails associations. there are some nice examples and schema pictures, that illustrate the problems:

如果您不确定,我建议您阅读有关rails关联的指南。有一些很好的例子和图式图片可以说明问题:

http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association

http://guides.rubyonrails.org/association_basics.html the-has_and_belongs_to_many-association

#4


0  

This is a many-to-many relation because in your example A is on both sides of the relationship.

这是一个多对多关系因为在你的例子中a在关系的两边。

#5


0  

I guess is a M2M relationship because in the table How many? more than one is related with Hoy many more than one.

我猜是M2M关系因为表格中有多少?一个以上的关系与一个以上的联系。

#1


5  

This is a many to many relation, as one account can edit many, and many accounts can edit one account.

这是一个多对多的关系,因为一个帐户可以编辑多个,许多帐户可以编辑一个帐户。

Example:

例子:

A can edit ABC  
B can edit AC 
C can edit BC

So A can be edited by multiple accounts (or belongs to multiple accounts), and can edit multiple accounts (or has multiple accounts)

A可以由多个账户(或属于多个账户)进行编辑,也可以编辑多个账户(或有多个账户)

#2


4  

Let's work through your problem and see how to identify the relationship.

让我们一起来解决你的问题,看看如何识别这种关系。

When you're writing the code that will check whether a user is authorized, you are going to have two main pieces of data: The user that is performing the edit and the user that is being edited. So, the question your application will be asking is "can user A edit user B?"

当您编写检查用户是否被授权的代码时,您将有两个主要的数据片段:执行编辑的用户和正在编辑的用户。因此,应用程序会问的问题是“用户A可以编辑用户B吗?”

Ignoring the semantics of relationships for a moment, think about how you'd look up the answer to that question:

忽略关系的语义,思考一下你是如何找到这个问题的答案的:

table can_edit:
  requestor_id | edited_id | permission
  =====================================
  User A       | User B    | YES
  User A       | User C    | YES
  User A       | User D    | YES
  User A       | User E    | NO
  User B       | User A    | YES
  User B       | User C    | YES
  User B       | User D    | NO
  etc...

Using this lookup table, you can determine who has access to edit whom. But, look again at what this table is describing. Since you're looking up the answer to a YES/NO question, you can express it more simply:

使用这个查找表,您可以确定谁有权编辑谁。但是,再看看这个表描述的是什么。既然你在寻找一个是或否问题的答案,你可以更简单地表达它:

table can_edit:
  requestor_id | edited_id | permission
  =====================================
  User A       | User B    | YES
  User A       | User C    | YES
  User A       | User D    | YES
  User B       | User A    | YES
  User B       | User C    | YES
  etc...

Here we've dropped the NO permissions. Now we can ask, "Do we have an entry in can_edit that matches our permission check?" If we perform a lookup and the row exists, then we can allow the access.

这里我们已经删除了NO权限。现在我们可以问,“can_edit中是否有与权限检查相匹配的条目?”如果执行查找,并且行存在,则可以允许访问。

Now, since the permission column will always be YES, it doesn't make much sense to include it. So now we have a table that is basically a list of user ID relationships.

既然权限列总是YES,那么包含它就没有多大意义了。现在我们有一个表基本上是用户ID关系的列表。

At this point we can draw out what the relationships look like using a database diagram:

此时,我们可以使用数据库图绘制出关系的样子:

users:
  user_id  <-----+
  email          |       can_edit:
  pass_digest    +-------- requestor_id
               +========== edited_id
users:         |
  user_id <====+
  email
  pass_digest

If we look at the example diagrams in the rails association guide, we can see that our diagram closely matches both has_many :through and has_and_belongs_to_many. These both describe many-to-many relationships.

如果我们查看rails关联指南中的示例图,就会发现我们的图与has_many:through和has_and_belongs_to_many非常匹配。它们都描述多对多关系。

As far as the implementation details go, you'll notice that both "can user A edit user B?" and "is user B a friend of user A?" are essentially the same question, just using different terms for the relationship. With that knowledge, you'll find that volumes have been written about how to define this self-referential has_many :through relationship, such as this excellent screencast by Ryan Bates.

就实现细节而言,您将注意到“用户A可以编辑用户B吗?”和“用户B是用户A的朋友吗?”这两个问题本质上是相同的,只是在关系中使用了不同的术语。有了这些知识,您将发现已经有很多关于如何定义这个自我引用的has_many:通过关系,例如Ryan Bates的出色的屏幕演示。

#3


2  

if you are not sure, i recommend that you read the guides about rails associations. there are some nice examples and schema pictures, that illustrate the problems:

如果您不确定,我建议您阅读有关rails关联的指南。有一些很好的例子和图式图片可以说明问题:

http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association

http://guides.rubyonrails.org/association_basics.html the-has_and_belongs_to_many-association

#4


0  

This is a many-to-many relation because in your example A is on both sides of the relationship.

这是一个多对多关系因为在你的例子中a在关系的两边。

#5


0  

I guess is a M2M relationship because in the table How many? more than one is related with Hoy many more than one.

我猜是M2M关系因为表格中有多少?一个以上的关系与一个以上的联系。