Rails,当有两个相等的字段时如何查询友谊模型:user_id &朋友 ID?

发布于 2024-12-21 22:56:39 字数 287 浏览 1 评论 0原文

我有一个这样的友谊模型:

Friendship (user_id, friend_id, status)

其中只有1条记录可以在用户A和用户B之间建立友谊。user_id是在friend_id上发起的用户。创建时状态为“待处理”。当friend_id批准或忽略时,该状态就会更新。

给个关系。如何获取用户的所有联系人?该查询需要同时查询user_id和friend_id,这就是@user.friendships不起作用的原因。这仅显示创建的朋友。

有想法吗?

I have a friendship model like so:

Friendship (user_id, friend_id, status)

Where there is only 1 record to establish a friendship between User A and User B. user_id is the user who initiated on friend_id. On create the status is "pending." When the friend_id approves or ignores, that status is updated.

Give the relationship. How can I get all of a user's contacts? The query needs to query across both user_id and friend_id, which is why @user.friendships does not work. That only shows the friends that the created.

Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月静好 2024-12-28 22:56:39

我构建了一个 Rails 应用程序,需要维护人与人之间的关系谱系。我所做的是使用 after_createafter_updateafter_destroy 触发器。我这样做的原因是我想要快速地进行人与人之间的搜索。效果很好。

创建代码如下所示:

def after_create(record)
  if ! record.relative.has_relative(record.person_id)
    new_relationship = Relationship.new(:relative => record.person, 
      :relationship => hantai(record.relative.gender, record.person.gender, record.relationship))
    record.relative.relationships << new_relationship
    record.relative.save
  end
 end

其中 Relationship 对象与您的 Friendship 对象相同,而本例中的 record 是人。

I built a rails app that needs to maintain a genealogy of relationships between people. What I did was create the equal and opposite map for each and every relationship, e.g. [a, b, father], [b, a, son] using the after_create, after_update and after_destroy triggers. The reason I did this was I wanted very fast search from person to person. Works great.

The create code looks like this:

def after_create(record)
  if ! record.relative.has_relative(record.person_id)
    new_relationship = Relationship.new(:relative => record.person, 
      :relationship => hantai(record.relative.gender, record.person.gender, record.relationship))
    record.relative.relationships << new_relationship
    record.relative.save
  end
 end

Where the Relationship object is the same as your Friendship object and the record in this case is the person.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文