简体   繁体   English

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

[英]In Ruby, what does “scoped” mean?

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. 这对谷歌来说太难了。

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." 我认为上述代码段的本质是:“如果提供了搜索参数,请创建一个限制返回记录的ActiveRecord查询。否则,返回一个空白的平板查询。” So "scoped" is a (weirdly named) object that represents a query that can be further refined before being eventually executed. 所以“scoped”是一个(奇怪命名的)对象,它表示一个可以在最终执行之前进一步细化的查询。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM