Embedded association validations are broken on updates when they depend on some parent's field

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Won't Fix
    • Priority: Major - P3
    • None
    • Affects Version/s: 8.0.8
    • Component/s: Associations
    • None
    • Ruby Drivers
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      Consider the following code that allows you to reproduce the regression, since version 8.0.8, probably caused by this commit:

      https://github.com/mongodb/mongoid/commit/cddda5f18c55c794cb905f3a271d621db88e6a21

      require 'bundler/inline'
      
      gemfile(true) do
        source "https://rubygems.org"
      
        gem "mongoid", "#{ENV["MONGOID_VERSION"] || '8.0.8'}"
      
        gem "minitest", require: "minitest/autorun"
      
        # non declared dependencies, no longer bundled with newer Rubies:
        gem "bigdecimal", "~> 3.1", require: false
        gem "mutex_m", "~> 0.2.0", require: false
        gem "base64", "~> 0.2.0", require: false
        gem "logger", "~> 1.6", require: false
        gem "ostruct", "~> 0.6.0", require: false
      end
      
      Mongoid.configure do |config|
        config.clients.default = {
          hosts: ['localhost:27017'],
          database: 'mongoid8_test',
        }
        config.log_level = :debug
      end
      
      class Author
        include Mongoid::Document
        embeds_many :posts, cascade_callbacks: true
      
        field :condition, type: Boolean
      end
      
      class Post
        include Mongoid::Document
        embedded_in :author
      
        validate :parent_condition_is_not_true
      
        def parent_condition_is_not_true
          return unless author&.condition
          errors.add :base, "Author condition is true"
        end
      end
      
      Author.delete_all
      Post.delete_all
      
      describe Author do
      
        describe "when author is not persisted" do
          it "validates author" do
            assert Author.new.valid?
            assert Author.new(posts: [Post.new]).valid?
            refute Author.new(condition: true, posts: [Post.new]).valid?
          end
        end
      
        describe "when author is persisted" do
          before do
            @author = Author.create posts: [Post.new]
          end
      
          it "author is initially valid" do
            assert @author.valid?
          end
      
          describe "when author condition becomes true" do
            before do
              @author.update_attributes condition: true
            end
      
            it "author becomes no longer valid because post is no longer valid" do
              refute @author.valid?
            end
          end
        end
      end 

      If you run it with `MONGOID_VERSION=8.0.7` all tests pass.

            Assignee:
            Jamis Buck
            Reporter:
            Rodrigo Rosenfeld Rosas
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: