raise_not_found_error=false + nested_attributes with invalid _id = NoMethodError

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Fixed
    • Priority: Unknown
    • 9.1.1
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Ruby Drivers
    • Not Needed
    • 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

      When Mongoid::Config.raise_not_found_error = false is set globally and a nested-attributes hash references a :_id for a child that doesn't exist on a has_and_belongs_to_many relation (or any relation that flows through Many#update_nested_relation's reparenting branch), the find lookup returns nil rather than raising Errors::DocumentNotFound. The nil is then passed straight to Many#update_document, which calls nil.update_attributes(attrs) and raises NoMethodError. See attached bug.rb. We have worked around this by doing a hack:

      module Mongoid
        # Mongoid 9.0.x's Many#update_document raises NoMethodError when an entry
        # references a non-existent _id and `Mongoid::Config.raise_not_found_error`
        # is false (the lookup returns nil rather than raising). Guard against it.
        module Association
          module Nested
            class Many
              def update_document(doc, attrs)
                return if doc.nil? # Added this
      
                delete_id(attrs)
                if association.embedded?
                  doc.assign_attributes(attrs)
                else
                  doc.update(attrs)
                end
              end
            end
          end
        end
      end
      

        1. bug.rb
          2 kB
          Kieran Pilkington (EXT)

            Assignee:
            Dmitry Rybakov
            Reporter:
            Kieran Pilkington (EXT)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: