i'ved been trying find ideal solution following in mongoid , found this. habtm mongoid following/follower
for reason, im not sure how optimum , post way in 2011.
if implemented way, followers , following ids kept in array. fine , quick small amount of followers. imagine if there thousands of followers, going through array may not quickest way if u search 1 item in array each time.
base on tutorial link, http://ruby.railstutorial.org/chapters/following-users following recommended put in relationship table, quick lookup when u need check on relationships.
my question code below great mongoid optimization ? need expert advise how u guys dealing relationships when there large amount of followers.
thanks.
class user include mongoid::document field :name, type: string has_and_belongs_to_many :following, class_name: 'user', inverse_of: :followers, autosave: true has_and_belongs_to_many :followers, class_name: 'user', inverse_of: :following def follow!(user) if self.id != user.id && !self.following.include?(user) self.following << user end end def unfollow!(user) self.following.delete(user) end end
you create actual follow
model (that therefore persisted in own collection) this:
class follow include mondoid::document belongs_to :user belongs_to :followed_user, class_name: 'user' end class user include mongoid::document field :name, type: string has_many :follows def follow!(user) follows.create(followed_user: user) end def unfollow!(user) follows.where(followed_user_id: user.id).destroy end end
Comments
Post a Comment