HABTM mongoid following/follower

后端 未结 1 598
猫巷女王i
猫巷女王i 2020-12-28 11:20

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 de

相关标签:
1条回答
  • 2020-12-28 11:34

    Following code worked fine for me (mongoid 2.3.x):

    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
    

    No inverse_class_name, no save calls, no special handling, but with exclusion of self-following.

    The reason is, that mongoid automatically uses dependent: nullify if not added to the relation statement. And with autosave: true the update of relationships get saved (and is only needed for following, because we do not alter followers directly). Without autosave option you need to add a save call in the methods, because mongoid doesn't automatically save relationship updates (since 2.0.0.x).

    I put the if-clause as block, so you can alter it with exception handling (else raise FooException).

    The .delete(user) is okay, also mentioned in the mongoid docs: http://mongoid.org/docs/relations/referenced/n-n.html (scroll down to "DEPENDENT BEHAVIOUR").

    0 讨论(0)
提交回复
热议问题