Mongoid ships with .push on a habtm, which sets a habtm relationship in both directions. Although delete will #delete an associated record, there's no documented way to delete only a relationship that I have seen. Is there a better way of doing this?
Is there a better way of ensuring uniqueness?
has_and_belongs_to_many :following, {class_name: 'User', inverse_of: :followers, inverse_class_name: 'User'}
has_and_belongs_to_many :followers, {class_name: 'User', inverse_of: :following, inverse_class_name: 'User'}
def follow!(user)
self.following.push(user) # this pushes the inverse as well
self.following_ids.uniq!
self.save!
user.follower_ids.uniq!
user.save!
end
def unfollow!(user)
self.following.delete(user.id)
self.save!
user.followers.delete(self.id)
user.save!
end