数据库 twitter 风格好友系统中黑名单功能如何设计
rails教程中有一章https://www.railstutorial.org/book/following_users是讲如何实现类似 twitter风格的可以follow,unfllow的好友系统的,数据库设计如下: 一个users表
id | integer
name | character varying(255)
relationships表
id | integer
follower_id | integer
followed_id | integer
model如下
class User < ActiveRecord::Base
has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy
has_many :following, through: :active_relationships, source: "followed"
has_many :passive_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy
has_many :followers, through: :passive_relationships, source: :follower
def follow(other_user)
active_relationships.create(followed_id: other_user.id)
end
def unfollow(other_user)
active_relationships.find_by(followed_id: other_user.id).destroy
end
def following?(other_user)
following.include? other_user
end
end
class Relationship < ActiveRecord::Base
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
end
在此基础上加上黑名单功能,我想到的方案是 在relationships表中加入一个状态(status)字段, 不过这样做之后逻辑变的复杂很多, 还有其他设计方案吗?