简体   繁体   English

Ruby on Rails活动记录协会,我该怎么做确认友谊(例如在Facebook上)

[英]Ruby on Rails Active Record Associations, how i can do confirmation friendship(like on facebook)

I study RoR and do application like facebook. 我学习RoR,并做类似Facebook的申请。 1 user send request to 2 user for friendship. 1位用户向2位用户发送了友谊请求。 Second user have confirm friendship that they will be a friends 第二位用户已确认友谊,他们将成为朋友

Please say to me have i can do more simple that i need. 请对我说我可以做些我需要的更简单的事情。

I have and do: tables: 我有做:表:

1. users
:id
:email
 (has_many :friendships, foreign_key: 'requester_id', dependent: :destroy
  has_many :requesters, through: :friendships, source: :requester
  has_many :responders, through: :friendships, source: :responder)
2. friendships
:id
:requester_id
:responder_id
 (belongs_to :requester, class_name: "User"
  belongs_to :responder, class_name: "User"
  has_one   :confirm_friendship)
3. confirm_friendships
:friendship_id
:confirm
 (belongs_to :friendship)

That users have friendship i do:
u1 = User.first
u2 = User.second
u1.friendships.create!(responder_id: other_user.id)
fr = Friendship.find_by(requester_id: u1.id, responder_id: u2.id)
conf = ConfirmFriendship.create(friendship_id: fr.id, confirm: true)

Sorry for my English. 对不起我的英语不好。 Thanks a lot for your Help. 非常感谢你的帮助。

Take a look at a number of the Rails based open source 'Social Networks' on Github. 看看Github上许多基于Rails的开源“社交网络”。 Here's a link( https://github.com/diaspora/diaspora/blob/develop/app/models/user.rb ) to the User model of Diaspora which provides a good example to study. 这是指向Diaspora的User模型的链接( https://github.com/diaspora/diaspora/blob/develop/app/models/user.rb ),它提供了一个很好的研究示例。

If you are looking for the simplest implementation possible I would say you could try the following where you can do away with the confirm_friendships model and place a confirmed? 如果您正在寻找最简单的实现方式,我会说您可以尝试以下操作,在其中可以取消confirm_friendships模型并放置confirmed? boolean column on the friendships table. 友谊表上的布尔值列。

Set friendships.confirmed? 确定friendships.confirmed? to default to false, and only once the responder accepts do you set it to true . 默认为false,并且只有响应者接受后,您才将其设置为true

This then allows you to filter user.friendships with a scope such as: 然后,您可以使用以下范围来过滤user.friendships

scope :confirmed, -> { where("friendships.confirmed? = ?", true) }

To separate out which friends are in the confirmed status and which are not. 区分哪些朋友处于确认状态,哪些没有。

Perhaps you could simplify the models with something along these lines? 也许您可以通过以下方式简化模型?

User
has_many: friendships

Friendships
belongs_to :requester, class_name: "User"
belongs_to :responder, class_name: "User"

It may be too simplified, but I hope something in this answer will be useful to you. 可能过于简化了,但我希望此答案对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM