我是一名新的 Rails 开发人员,我正在尝试对用户-朋友关系(Facebook 风格)进行建模,例如它同时具有朋友和订阅系统。我在一些博客和书籍上读过它,但我觉得我找不到一个非常合适的实现。
我希望每个关系对应的数据库中只有 1 个条目,所以我用字段(user_id,friend_id,status,type)对我的关系表建模,其中 type ={friend,subscriber} 和 status = {accepted,hold }
我想实现所有功能,如 send_request、accept_request、cancel_request、unfriend、delete_request、subscribe、unsubscribe。
接下来我希望所有功能都遵循抽象规则。像 send_request 应该像 current_user.send_request(@user) 而不是像 send_request(@user, @friend) 那样工作,并在那里写下所有的 sql。
我相信这样的实现可以帮助我将关系扩展到更多类型,如“家人”“最好的朋友”等。
我看了很多,但找不到正确实现 facebook 风格的友谊模型。请告诉我一些资源,我可以从中获得一些帮助,或者如果可能的话帮助我编写代码。我正在以我开始编码的方式发布我的用户模型的代码,但我觉得可能有更好的解决方案。请通过一些见解。我知道这个问题不是很中肯和主观的。管理员:请原谅违反了 Stackoverflow 的规则。
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
has_one :profile, :dependent => :destroy
has_many :relationships, :foreign_key => :user_id, :dependent => :destroy
has_many :reverse_relationships, :foreign_key => "friend_id", :class_name => "Relationship", :dependent => :destroy
has_many :direct_friends, :through => :relationships, :conditions => "status = 'accepted'", :source => :friend
has_many :reverse_friends, :through => :reverse_relationships, :conditions => "status = 'accepted'", :source => :user
has_many :requested_friends, :through => :reverse_relationships,
:source => :friend, :conditions => "status = 'hold'", :order => :created_at
has_many :pending_friends, :through => :relationships,
:source => :user, :conditions => "status = 'hold'", :order => :created_at
attr_accessible :email, :password, :password_confirmation, :remember_me
def friends
direct_friends | reverse_friends
end
.
.
.