Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-5079

clear has_many association locally after destroy_all

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 8.0.1, 7.4.1, 7.3.5
    • Affects Version/s: 7.0.12, 7.2.2
    • Component/s: Associations
    • Labels:
      None
    • Environment:
      Rails 5.2.4.4
      Mongoid 7.0.12
    • Minor Change

      Given those classes :

      class Article
        include Mongoid::Document
        has_many :comments
      end
      
      class Comment
        include Mongoid::Document
        belongs_to :article
      end 

      When I create a comment with an article, the number of comments in the article isn't updated after destroy_all. The clear does the job.

          article = Article.create
          comment = Comment.create(article: article)
      
          pp article.comments.length # => 1
          article.comments.destroy_all
          pp article.comments.length # => Continues to give 1 and should give 0
          article.comments.clear
          pp article.comments.length # => 0
      

      If I add article.reload after the creation of the comment the behavior is correct. This problem isn't in Mongoid 6.2.

      delete_all also currently does not clear the association - this may be the correct/desired behavior and when this ticket is worked on tests should be added to this effect if there aren't already.

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            guirec.corbel@gmail.com Guirec Corbel
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: