许多一对多自联接的轨道? [英] Many-to-many self join in rails?
问题描述
Rails文档提供href="http://guides.rubyonrails.org/association_basics.html#self-joins" rel="nofollow">很好的解释如何处理自连接,其中一个
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?
推荐答案
A 用户可以有很多:
- 在其能力追随者followee
- 在其作为追随者的能力followees。
下面是如何在$ C $下 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
下面是如何在$ C $下 follow.rb :
Here's how the code for 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. followee_follows
:follower_follows 和要使用该轧机运行(无环)组织,作为一个例子,一个团队可能有很多:
播放器
到:合约
。这是没有什么不同的播放下,谁可能有许多:团队
到:合约
还有(对这种播放的职业生涯)。
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).
但在这种情况下,只有一个指定的模型存在(即用户),命名通:相同的关系(如通过:遵循
)会导致对不同的用例(或接入点的命名冲突进入)连接表。 :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.
现在,一个用户可以有多个:追随者
到: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的:关注,其中这样的用户是跟随者(即foreign_key:follower_id
)通过:如用户的:followee_follows。 - 要确定用户的:追随者(在一个
@ user.followers
调用数据库),Rails的可能现在看每例如将class_name的:跟,其中如用户是的followee(即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屋!