ExportXMLWordPrintableJSON

    • Type: Bug
    • Resolution: Fixed
    • Priority: Major - P3
    • 9.1.3
    • Affects Version/s: 9.1.0, 9.1.1, 9.0.12, 8.1.13, 8.0.13, 7.6.2
    • Component/s: None
    • None
    • None
    • Fully Compatible
    • Ruby Drivers
    • Not Needed
    • None
    • None
    • None
    • None
    • None
    • None

      The referenced has-many association proxy accepts an optional conditions Hash on its two bulk delete methods and is documented to delete the associated documents matching those conditions. It builds the selector by merging the association selector over the caller's Hash with a right-biased merge, so a String key in the conditions that also appears in the association selector is replaced by the association value instead of being intersected with it. The caller's Hash is mutated in place as well.

        # lib/mongoid/association/referenced/has_many/proxy.rb:539-541 (master, v9.1.1)
        selector = conditions || {}
        selector.merge!(criteria.selector)
        # line 558 then passes selector to the class level delete
      

      The association selector carries the child default scope, the association scope option, the foreign key, the polymorphic type and, for has-and-belongs-to-many (whose proxy inherits this method), the primary key list. Symbol keys do not collide because selector keys are Strings, and the class level delete then conjoins them. An ActiveSupport indifferent access Hash, which is what Rails permitted params produce, stores String keys and does collide.

      Repro:

      class Account
        include Mongoid::Document
        has_many :items, scope: -> { where(:state.in => %w[draft open]) }
      end
      
      class Item
        include Mongoid::Document
        field :state, type: String
        belongs_to :account
      end
      
      acct = Account.create!
      Item.create!(account: acct, state: 'open')
      Item.create!(account: acct, state: 'open')
      Item.create!(account: acct, state: 'draft')
      acct = Account.find(acct.id)
      
      acct.items.delete_all(state: 'draft')       # => 1, symbol key, correct
      acct.items.delete_all('state' => 'draft')   # => 3, string key, the open items are deleted too
      

      Wire command in the second case:

        { "state" => { "$in" => ["draft", "open"] }, "account_id" => acct.id }
      

      The embedded proxy and the class level delete build their selector through where and are correct:

        # lib/mongoid/association/embedded/embeds_many/proxy.rb:584-586
        # lib/mongoid/persistable/deletable.rb:129-132
      

      The merge has been there since 2011 (76d8692e5) and was only reformatted by 91cc55dac.

      Fix: build the selector through the association criteria so the conditions are conjoined, and stop mutating the caller's Hash. Verified locally that this yields a conjunctive selector for String and indifferent access keys:

        selector = criteria.where(conditions || {}).selector
        # => {"state" => {"$in" => [...]}, "account_id" => ..., "$and" => [{"state" => "draft"}]}
      

      Keep using that selector for the in-memory pruning and the regexp budget branch, which both take a selector Hash.

      Specs: for both bulk delete methods, with String, Symbol and indifferent access keys, against an association scope, a child default scope, a foreign key condition and has-and-belongs-to-many ids; assert the same count and remaining documents as the equivalent where chain, and that the caller's Hash is unchanged after the call.

      Affected: all released lines (9.1.0, 9.1.1, 9.0.x, 8.1.x, 8.0.x, 7.x) and master.

            Assignee:
            Jamis Buck
            Reporter:
            Dmitry Rybakov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: