如何在应用程序中“隐藏用户" [英] How to 'hide-users' in application

查看:46
本文介绍了如何在应用程序中“隐藏用户"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个带有用户/用户配置文件的应用程序.目前,用户可以查看其他人的个人资料、发送消息和收藏用户个人资料.我还想添加允许用户隐藏用户"个人资料的功能,这样他们就不会在搜索或任何内容中再次看到该用户.并在设置"中提供选项以取消隐藏"用户.

So I have an application with users/user-profiles. Presently, users can view others profiles, send messages, and favorite user profiles. I'd like to also add the feature to allow users to 'hide-user' profiles so that they won't see the user ever again in search, or anything. And provide the option in 'settings' to 'un-hide' the user as well.

我该怎么做?我不知道从哪里开始使用此功能.

How might I do this? I haven't a clue as to where to begin with this feature.

(如果您需要任何代码示例、模型、控制器,请询问,我很乐意提供)

(If you need any code examples, models, controllers please ask and I will happily provide)

亲切的问候,桑尼

推荐答案

我相信一种方法是通过 has_many :通过用户和隐藏用户之间的关联来创建 many_to_many 关系.同样,用户和隐藏者(即隐藏用户的用户)之间的另一个 many_to_many 关系.最后,你应该能够做这样的事情:

I believe one approach would be to create a many_to_many relationship via the has_many :through association between users and hidden users. And likewise another many_to_many relationship between users and hiders (i.e. users who are hiding the user). In the end, you should be able to do something like this:

some_user.hidden_​​userssome_user.hiders(即隐藏某个用户的所有用户).大多数情况下,您可能不需要第二个.

some_user.hidden_users and some_user.hiders (i.e. all the users that are hiding some user). You probably won't need the second one most of the time.

因此,首先要做的是创建一个名为 hiders_hidden_​​users(或您想要的任何名称)的连接表,其中仅包含两个字段:hider_idhidden_​​user_id.

So the first thing to do would be to create a join table called hiders_hidden_users (or whatever you want) which would contain only two fields: hider_id and hidden_user_id.

请注意,实际上,这些 id 都将引用用户记录.您的 HidersHiddenUser 模型将如下所示:

Note, that in reality, those ids will both refer to a User record. Your HidersHiddenUser model will look something like this:

class HidersHiddenUsers < ActiveRecord::Base
  belongs_to :hidden_user, class_name: "User", foreign_key: "hidden_user_id"
  belongs_to :hider, class_name: "User", foreign_key: "hider_id"
end

然后,您需要在 User 类中设置关系:

Then, you need to set up the relationships in the User class:

class User < ActiveRecord::Base
  has_many :hiders_join, class_name: "HidersHiddenUser", foreign_key: "hider_id"
  has_many :hidden_users_join, class_name: "HidersHiddenUser", foreign_key: "hidden_user_id" 

  has_many :hiders, through: :hiders_join
  has_many :hidden_users, through: :hidden_users_join
end

注意,在写与连接表的 has_many 关系时,必须指定类名和外键.请注意,您还必须使用同一模型两次指定关系.

Note, that you have to specify the class name and foreign key when writing the has_many relationship with the join table. Note, also that you have to specify the relationship twice with the same model.

然后,假设 some_user 想要隐藏 user1 和 user2.您需要做的就是这样:

Then, say some_user wants to hide user1 and user2. All you would need to do is something like this:

some_user.hidden_users << user1
some_user.hidden_users << user2

虽然我没有测试过这个,但我相信它应该可以工作.

Although I have not tested this, I believe it should work.

这篇关于如何在应用程序中“隐藏用户"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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