Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoid self reference with properties for users friendships status

Using Mongo and Rails, I would to build a friendship system like facebook: - Before making the friendship, the use must accept the friendship request

I found a lots of code to do the relationship but never with a relation's property...

Do you have any idea or clue how to do that to be "respectful" of the NoSQL concept

Thank you for your help

like image 931
DJYod Avatar asked Dec 04 '22 06:12

DJYod


2 Answers

Just use two models, something like this:

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

Does it sound good? Hope this helps!

like image 126
ShogunPanda Avatar answered Dec 15 '22 00:12

ShogunPanda


I had to put in my User model:

has_many :friendships, :inverse_of => :owner

Check out associations in the documentation http://mongoid.org/en/mongoid/docs/relations.html#common

like image 37
Kevin Bahr Avatar answered Dec 14 '22 22:12

Kevin Bahr