Rails的Mongoid清除标准

Rails的Mongoid清除标准

本文介绍了Rails的Mongoid清除标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mongoid :: Paranoia将默认范围添加到生成条件的模型中

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>

我可以找到带有生成的Model.deleted的已删除文档,

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不起作用

推荐答案

尝试一下(这种hack):

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")

这篇关于Rails的Mongoid清除标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 01:15