Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails mongoid clear criteria

Mongoid::Paranoia adds a default scope to the model which generates the criteria

#<Mongoid::Criteria
  selector: {:deleted_at=>{"$exists"=>false}},
  options:  {},
  class:    Line,
  embedded: false>

I can find deleted documents with Model.deleted which generates,

#<Mongoid::Criteria
  selector: {:deleted_at=>{"$exists"=>true}},
  options:  {},
  class:    Line,
  embedded: false>

How can i override this so i can search both deleted and non-deleted documents.

PS Model.unscoped does not work

like image 881
Dipil Avatar asked Sep 09 '11 06:09

Dipil


2 Answers

Try this(its kind of hack):

class User
  include Mongoid::Document
  include Mongoid::Paranoia

  def self.ignore_paranoia
    all.tap {|criteria| criteria.selector.delete(:deleted_at)}
  end
end

# ignore paranoia is defined on model, so can't chain it to criteria or scopes
# but it returns criteria, so can chain other scope and criteria to it
User.ignore_paranoia.some_scope.where(:whatever => "my_custom_value")
like image 187
rubish Avatar answered Oct 15 '22 21:10

rubish


I've taken to using:

 def self.all!
   Mongoid::Criteria.new self
 end

but self.unscoped seems to work too.

like image 22
mdan Avatar answered Oct 15 '22 23:10

mdan