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

delete_all is homicidal

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

      Guess what this does?

      Book.delete_all(:title => "The Great Gatsby")
      

      A quick check for :conditions would be appreciated. Apparently I'm not the only one to encounter this issue:

      http://www.themomorohoax.com/2010/10/27/mongoids-delete-all-is-way-too-epic

      Here's my amusing workaround, for posterity:

      module Mongoid
        module Persistence
          module ClassMethods
            alias :delete_all_old :delete_all
            
            def delete_all(conditions = nil)
              selector = (conditions || {})[:conditions] || {}
              selector.merge!(:_type => name) if hereditary?
      
              if collection.find(selector).count == collection.count
                raise "Are you crazy?!?! Did you forget :conditions?"
              end
      
              delete_all_old(conditions)
            end
          end
        end
      end
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            gurgeous Adam Doppelt
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: