Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 重新定义关注的默认范围(在Rails中)_Ruby On Rails_Ruby_Scope_Activesupport Concern - Fatal编程技术网

Ruby on rails 重新定义关注的默认范围(在Rails中)

Ruby on rails 重新定义关注的默认范围(在Rails中),ruby-on-rails,ruby,scope,activesupport-concern,Ruby On Rails,Ruby,Scope,Activesupport Concern,我想在model concern中重新定义default\u范围,但我得到的是: 您试图在模型“Product”上定义一个名为“default\u scope”的作用域,但活动记录已定义了一个同名的类方法。 module SoftDeletable extend ActiveSupport::Concern included do scope :default_scope, -> { where(deleted_at: nil) } end end 是的,我知道这方

我想在model concern中重新定义
default\u范围
,但我得到的是:

您试图在模型“Product”上定义一个名为“default\u scope”的作用域,但活动记录已定义了一个同名的类方法。

module SoftDeletable
  extend ActiveSupport::Concern

  included do
    scope :default_scope, -> { where(deleted_at: nil) }
  end
end
是的,我知道这方面有很多优点,但问题不在于此。
您知道如何设置相关的
默认范围吗


谢谢

这个问题实际上与将范围定义为关注点的一部分无关。如果试图从模型本身中定义名为
default\u scope
的范围,您会看到相同的错误

要设置默认范围,请使用:

范围
用于创建自己的命名范围,例如:

scope :non_deleted, -> { where(deleted_at: nil) }
会允许你写作吗

`Model.non_deleted.where....`

因此,您问题中的代码试图创建一个名为
default\u scope
的作用域,但错误消息表明,作用域
Model.default\u scope…
将与用于设置默认作用域的现有
default\u scope
方法冲突。

Ok,那么
scope:default\u scope,->{where(deleted\u at:nil)}
default\u scope{where(deleted\u at:nil)}
之间有什么区别呢?第一个是定义一个新范围,第二个是覆盖现有范围?添加了一些详细信息
`Model.non_deleted.where....`