在Ruby中,“作用域”是什么意思?

时间:2021-02-04 22:34:52

I've come across this code:

我遇到过这段代码:

def self.search(search)
    if search
      where(['title LIKE ? OR description LIKE ? ', "%#{search}%", "%#{search}%"])  
    else
      scoped
    end
  end

What does the "scoped" statement refer to? It's surprisingly hard to Google.

“范围”声明指的是什么?这对谷歌来说太难了。

1 个解决方案

#1


3  

I think the essence of the above snippet is: "if a search parameter is provided, create an ActiveRecord query that restricts the records returned. Otherwise, return a blank slate query." So "scoped" is a (weirdly named) object that represents a query that can be further refined before being eventually executed.

我认为上述代码段的本质是:“如果提供了搜索参数,则创建一个限制返回记录的ActiveRecord查询。否则,返回一个空白的平板查询。”所以“scoped”是一个(奇怪命名的)对象,它表示在最终执行之前可以进一步细化的查询。

http://guides.rubyonrails.org/active_record_querying.html

#1


3  

I think the essence of the above snippet is: "if a search parameter is provided, create an ActiveRecord query that restricts the records returned. Otherwise, return a blank slate query." So "scoped" is a (weirdly named) object that represents a query that can be further refined before being eventually executed.

我认为上述代码段的本质是:“如果提供了搜索参数,则创建一个限制返回记录的ActiveRecord查询。否则,返回一个空白的平板查询。”所以“scoped”是一个(奇怪命名的)对象,它表示在最终执行之前可以进一步细化的查询。

http://guides.rubyonrails.org/active_record_querying.html