DJYod
DJYod

Reputation: 237

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

Upvotes: 4

Views: 1314

Answers (2)

Kevin Bahr
Kevin Bahr

Reputation: 61

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

Upvotes: 6

ShogunPanda
ShogunPanda

Reputation: 1056

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!

Upvotes: 11

Related Questions