Rails,当有两个相等的字段时如何查询友谊模型:user_id &朋友 ID?
我有一个这样的友谊模型:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我构建了一个 Rails 应用程序,需要维护人与人之间的关系谱系。我所做的是使用
after_create
、after_update
和after_destroy
触发器。我这样做的原因是我想要快速地进行人与人之间的搜索。效果很好。创建代码如下所示:
其中
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
andafter_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:
Where the
Relationship
object is the same as yourFriendship
object and therecord
in this case is the person.