具有用户友谊状态属性的Mongoid自引用

DJY*_*Yod 4 entity-relationship many-to-many ruby-on-rails mongodb mongoid

使用Mongo和Rails,我会建立一个像facebook这样的友谊系统: - 在建立友谊之前,使用必须接受友谊请求

我找到了很多代码来做关系,但从来没有关系的属性......

您是否有任何想法或线索如何做到这一点,以"尊重"NoSQL概念

谢谢您的帮助

小智 11

只需使用两个模型,如下所示:

class User
  include Mongoid::Document
  has_many :friendships
end

class Friendship
  include Mongoid::Document
  belongs_to :owner, :class_name => "User"
  belongs_to :friend, :class_name => "User"
  field :pending, :type => Boolean, :default => true
end
Run Code Online (Sandbox Code Playgroud)

听起来不错吗?希望这可以帮助!


小智 6

我不得不放入我的用户模型:

has_many :friendships, :inverse_of => :owner
Run Code Online (Sandbox Code Playgroud)

查看文档http://mongoid.org/en/mongoid/docs/relations.html#common中的关联