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

Documents in associations aren't updated after we build new records for embedded associations

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 7.0.12
    • Component/s: Associations
    • None

      Givens those classes :

      class Article
        include Mongoid::Document
        embeds_many :comments, order: :body.asc
      end
      
      class Comment
        include Mongoid::Document
        embedded_in :article
      
        field :body, type: String
      end
      

      This code doesn't give expected results :

      article = Article.new
      article.comments.criteria # => works without this line
      
      article.comments.build(body: 'test')
      pp article.comments.count # => gives 0 instead of 1
      pp article.comments.where(body: 'test').count # => gives 0 instead of 1
      pp article.comments.context.documents # gives an empty array
      

      This happens because in article.comments.criteria we load article.comments.criteria.documents and those values are memoized. Elements we add later in the association are ignored and article.comments.criteria.documents always gives the same array. Those values have to be cleaned when we add or remove elements in documents.

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            guirec.corbel@gmail.com Guirec Corbel
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: