用户有很多:用户,还是我必须使用另一种方式来建立基于朋友的社交网络? [英] user has many :users, or must I use another way for a friend based social network?

查看:60
本文介绍了用户有很多:用户,还是我必须使用另一种方式来建立基于朋友的社交网络?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Rails 中创建一个小型社交网络,人们可以在其中将彼此添加为朋友.我创建了一个名为 'user' 的模型,其中包含电子邮件、带有密码盐等的强 md5 哈希.

I'm creating a little social network in Rails, where people can add eachother as a friend. I have created a model called 'user', which contains, e-mail, strong md5 hash with salt of password etc.

如何创建类似于将其他用户添加为好友的选项?是否有可能在用户模型中有类似 has_many_and_belongs_to :user 的东西?所以一个用户有很多用户,属于很多用户.或者我应该使用另一种方式,比如添加一个具有 user1s_id:integeruser2s_id:integer 的友谊模型?

How do I create something like an option to add another user as a friend? Is it possible to have something like has_many_and_belongs_to :user in the user model? So a user has many users and belongs to many users. Or should I use another way, like adding a friendship model which has user1s_id:integer and user2s_id:integer?

推荐答案

我会建议一个用户有很多关系,这会让你在未来可以自由添加新类型的关系.

I would suggest that a user has many relationships, this would leave you free to add new types of relationships in the future.

因此,您从一个用户到另一个用户"关系表(最低限度,只有两个 ID)开始,然后在将来您可以添加一个用户到一个组"关系表(一旦您创建了一组当然!)

So you start with a "one user to another user" relationship table (minimally, just two IDs) and then in the future you could add a "one user to a group" relationship table (once you've created groups of course!)

这篇关于用户有很多:用户,还是我必须使用另一种方式来建立基于朋友的社交网络?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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