多对多自我加入导轨? [英] Many-to-many self join in rails?

查看:17
本文介绍了多对多自我加入导轨?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails 文档提供了关于如何处理自联接的很好的解释只需要 has_many-belongs_to 关系.在这个例子中,一个员工(作为经理)可以有很多员工(每个,作为一个下属).

Rails documentation provides a nice explanation of how to handle a self join where only a has_many-belongs_to relationship is required. In the example, an employee (as a manager) can have many employees (each, as a subordinate).

但是,您如何处理 has_many-has_many 自连接(我听说它被称为双向循环关联)?

However, how do you handle a has_many-has_many self join (which I've heard referred to as a bi-directional looped association)?

例如,如果一个员工可以有很多下属,作为经理,并且有很多经理,作为下属,你如何处理?

For example, how do you handle the situation in which an employee can have many subordinates, in its capacity as manager, and also have many managers, in its capacity as subordinate?

或者说,一个用户在哪里可以关注很多用户,也可以被很多用户关注?

Or, in other words, where a user can follow many users and be followed by many users?

推荐答案

一个用户可以有多个:

  • 作为被关注者的关注者
  • 以追随者的身份追随者.

user.rb 的代码如下所示:>

Here's how the code for user.rb might look:

class User < ActiveRecord::Base
  # follower_follows "names" the Follow join table for accessing through the follower association
  has_many :follower_follows, foreign_key: :followee_id, class_name: "Follow" 
  # source: :follower matches with the belong_to :follower identification in the Follow model 
  has_many :followers, through: :follower_follows, source: :follower

  # followee_follows "names" the Follow join table for accessing through the followee association
  has_many :followee_follows, foreign_key: :follower_id, class_name: "Follow"    
  # source: :followee matches with the belong_to :followee identification in the Follow model   
  has_many :followees, through: :followee_follows, source: :followee
end

以下是 follow.rb 的代码:

class Follow < ActiveRecord::Base
  belongs_to :follower, foreign_key: "follower_id", class_name: "User"
  belongs_to :followee, foreign_key: "followee_id", class_name: "User"
end

需要注意的最重要的事情可能是 user.rb 中的术语 :follower_follows:followee_follows.以工厂运行(非循环)关联为例,一个团队可能有许多:玩家:contracts.这对于玩家来说没有什么不同,他可能有很多:teams:contracts(在这样的Player 的职业生涯).

The most important things to note are probably the terms :follower_follows and :followee_follows in user.rb. To use a run of the mill (non-looped) association as an example, a Team may have many :players through :contracts. This is no different for a Player, who may have many :teams through :contracts as well (over the course of such Player's career).

但在这种情况下,只有一个命名模型存在(即一个用户),命名通过:相同的关系(例如through::follow)将导致连接表的不同用例(或访问点)的命名冲突.创建 :follower_follows:followee_follows 是为了避免这种命名冲突.

But in this case, where only one named model exists (i.e. a User), naming the through: relationship identically (e.g. through: :follow) would result in a naming collision for different use cases of (or access points into) the join table. :follower_follows and :followee_follows were created to avoid such a naming collision.

现在,一个用户可以有很多:followers:follower_follows,还有很多:followees>:followee_follows:

Now, a User can have many :followers through :follower_follows and many :followees through :followee_follows:

  • 为了确定用户的:followees(根据对数据库的@user.followees 调用),Rails 现在可以查看 class_name 的每个实例:Followees",其中此类用户是关注者(即 foreign_key: :follower_id),通过:此类 User 的 :followee_follows.
  • 为了确定用户的:followers(基于对数据库的@user.followers 调用),Rails 现在可以查看 class_name 的每个实例:Follow",其中此类用户是关注者(即foreign_key: :followee_id),通过:此类用户的:follower_follows.
  • To determine a User’s :followees (upon an @user.followees call to the database), Rails may now look at each instance of class_name: "Follow" where such User is the the follower (i.e. foreign_key: :follower_id) through: such User’s :followee_follows.
  • To determine a User’s :followers (upon an @user.followers call to the database), Rails may now look at each instance of class_name: "Follow" where such User is the the followee (i.e. foreign_key: :followee_id) through: such User’s :follower_follows.

这篇关于多对多自我加入导轨?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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