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

delete_all on scoped association does not delete the documents, mongoid 3.0.19

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.1.0
    • Affects Version/s: None
    • Component/s: None

      upgraded from 2.4.3 to 3.0.19 recently and some delete_all method calls against associations don't delete the records anymore. Here is my analysis.

      Mongoid: 3.0.19, Rails 3.2.11, Ruby 1.9.3p327

      Sample Code

      class Subscription
        include Mongoid::Document
        include Mongoid::Timestamps
        include Mongoid::Paranoia
      
        has_many :invoices
      
        # worked with mongoid 2.4.3
        # no longer works with mongoid 3.0.19
        def clear_invoices!
          invoices.recurring.pending.delete_all
        end
      
        # works with mongoid 3.0.19
        def clear_invoices_new!
          invoices.delete_all(type: 'recurring', status: 'pending')
        end
      end
      
      class Invoice
        include Mongoid::Document
        include Mongoid::Timestamps
        include Mongoid::Paranoia
      
        field :type, type: String, default: 'recurring'
        field :status, type: String, default: 'pending'
      
        belongs_to :subscription
      
        scope :recurring, where(type: 'recurring').order_by(:created_at.desc)
        scope :pending, where(status: 'pending').order_by(:created_at.desc)
      end
      

      docs say i can call delete_all on a criteria:

       ruby
      band.members.
        where(name: "Fletch").delete_all
      

      quick console test:

      irb(main):032:0> sub.invoices.count
        MOPED: 127.0.0.1:27017 COMMAND      database=mongoid command={:count=>"invoices", :query=>{"deleted_at"=>nil, "subscription_id"=>"51003ff7ab0a5d3f10000001"}} (0.7582ms)
      => 1
      
      
      irb(main):033:0> sub.clear_invoices!
        MOPED: 127.0.0.1:27017 COMMAND      database=mongoid command={:count=>"invoices", :query=>{"deleted_at"=>nil, "subscription_id"=>"51003ff7ab0a5d3f10000001", "type"=>"recurring", "status"=>"pending"}} (0.7679ms)
        MOPED: 127.0.0.1:27017 DELETE       database=mongoid collection=invoices selector={"$query"=>{"deleted_at"=>nil, "subscription_id"=>"51003ff7ab0a5d3f10000001", "type"=>"recurring", "status"=>"pending"}, "$orderby"=>{"created_at"=>-1}} flags=[] (0.2458ms)
      => 1
      irb(main):034:0> sub.invoices.count
        MOPED: 127.0.0.1:27017 COMMAND      database=mongoid command={:count=>"invoices", :query=>{"deleted_at"=>nil, "subscription_id"=>"51003ff7ab0a5d3f10000001"}} (0.8352ms)
      => 1
      
      
      irb(main):035:0> sub.clear_invoices_new!
        MOPED: 127.0.0.1:27017 COMMAND      database=mongoid command={:count=>"invoices", :query=>{:type=>"recurring", :status=>"pending", "deleted_at"=>nil, "subscription_id"=>"51003ff7ab0a5d3f10000001"}} (1.0450ms)
        MOPED: 127.0.0.1:27017 DELETE       database=mongoid collection=invoices selector={:type=>"recurring", :status=>"pending", "deleted_at"=>nil, "subscription_id"=>"51003ff7ab0a5d3f10000001"} flags=[] (0.1462ms)
        MOPED: 127.0.0.1:27017 QUERY        database=mongoid collection=invoices selector={"deleted_at"=>nil, "subscription_id"=>"51003ff7ab0a5d3f10000001"} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.3572ms)
      => 1
      irb(main):036:0> sub.invoices.count
        MOPED: 127.0.0.1:27017 COMMAND      database=mongoid command={:count=>"invoices", :query=>{"deleted_at"=>nil, "subscription_id"=>"51003ff7ab0a5d3f10000001"}} (0.8490ms)
      => 0
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            ckhall ckhall
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: