gpt4 book ai didi

ruby-on-rails - ruby on rails 在事件记录中获得双向好友关系?

转载 作者:行者123 更新时间:2023-12-04 05:54:33 25 4
gpt4 key购买 nike

我试图弄清楚如何建立双向的双向关系,即:

user_id  friend_id
1 2
2 1

在上面的用户 1 和用户 2 将是 friend ,如果两者 user_id = 1friend_id = 2friend_id = 2user_id = 2就像 table 上的 friend 一样。如何统计 ActiveRecord 中所有的 2-way 相互关系?

最佳答案

您正在寻找的是 has_and_belongs_to_many relationship :

class User < ActiveRecord::Base
has_and_belongs_to_many :friends, :class_name => "User",
:foreign_key => "this_user_id",
:association_foreign_key => "other_user_id"
end

示例来自 §4.4.2.1 .

关于ruby-on-rails - ruby on rails 在事件记录中获得双向好友关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9671829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com