在 Rails 中覆盖 default_scope [英] Overriding default_scope in Rails
问题描述
在我的 Post.rb 模型中,我有 default_scope :conditions =>{:已删除 =>'假'}
In my Post.rb model, I have default_scope :conditions => {:deleted => 'false'}
但如果我尝试运行 Post.find(:all, :conditions => "deleted='false'")
,它不会返回任何内容.就好像 default_scope 优先于一切.
But if I try to run Post.find(:all, :conditions => "deleted='false'")
, it won't return anything. It's as if the default_scope takes precedence over everything.
我想要这样,当我执行 Post.find()
时,它不会返回已删除的帖子,但我也希望能够在需要时访问它们.在我的查询或 Rails 模型中需要更改什么?
I want it so that when I do Post.find()
it doesn't return deleted posts, but I'd also like to be able to access them if I need to. What needs to be changed in either my query or my Rails model?
谢谢.
推荐答案
with_exclusive_scope
是protected
,所以你必须创建一个类方法:
with_exclusive_scope
is protected
, so you have to create a class method:
def self.include_deleted_in
Event.with_exclusive_scope { yield }
end
然后在您的控制器调用中
then in your controller call
Post.include_deleted_in { Post.find(:all) }
这篇关于在 Rails 中覆盖 default_scope的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!