-, Mongoid , . , , :
class User
include Mongoid::Document
field :friends, :type => Array, :default => []
def make_friends(friend)
self.add_friend(friend)
friend.add_friend(self)
end
def friends
ids = read_attribute :friends
ids.map { |id| User.find(id)}
end
def is_friends_with? other_user
ids = read_attribute :friends
ids.include? other_user.id
end
protected
def add_friend(friend)
current = read_attribute :friends
current<< friend.id
write_attribute :friends,current
save
end
end