Ruby-on-rails rails 中的 scope/named_scope 是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4869994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:16:52  来源:igfitidea点击:

What is scope/named_scope in rails?

ruby-on-railsnamed-scope

提问by Ziggy

I've recently started an internship. My employer uses ruby on rails, and I frequently encounter new syntax that I need to look up to understand. I've googled around for a good explanation of named_scope, but what I've found so far is mostly blog posts giving high praise for it, rather a straight definition or introduction.

我最近开始实习。我的雇主在 Rails 上使用 ruby​​,我经常遇到需要查找才能理解的新语法。我已经在 google 上搜索了对 named_scope 的一个很好的解释,但到目前为止我发现的主要是对它给予高度赞扬的博客文章,而不是直接的定义或介绍。

What exactly is named_scope (now simply called scope) in ruby on rails?

ruby on rails 中的named_scope(现在简称为scope)到底是什么?

回答by Michael Sch?fermeyer

A scope is a subset of a collection. Sounds complicated? It isn't. Imagine this:

范围是集合的子集。听起来很复杂?不是。想象一下:

You have Users. Now, some of those Users are subscribed to your newsletter. You marked those who receive a newsletter by adding a field to the Users Database (user.subscribed_to_newsletter = true). Naturally, you sometimes want to get those Users who are subscribed to your newsletter.

你有用户。现在,其中一些用户订阅了您的时事通讯。您通过向用户数据库添加一个字段来标记接收时事通讯的人 (user.subscribed_to_newsletter = true)。当然,您有时希望获得订阅您的时事通讯的用户。

You could, of course, always do this:

当然,您可以始终这样做:

User.where(subscribed_to_newsletter: true).each do #something

Instead of always writing this you could, however, do something like this.

然而,您可以做这样的事情,而不是总是写这个。

#File: users.rb
class User < ActiveRecord::Base
  scope :newsletter, where(subscribed_to_newsletter: true)
  #yada yada
end

If you're using Rails 4 or newer, do this instead:

如果您使用的是Rails 4 或更新版本,请改为执行以下操作:

#File: users.rb
class User < ActiveRecord::Base
  scope :newsletter, -> { where(subscribed_to_newsletter: true) }
  #yada yada
end

This allows you to access your subscribers by simply doing this:

这允许您通过简单地执行以下操作来访问您的订阅者:

User.newsletter.each do #something

This is a very simple example but in general scopes can be very powerful tools to easy your work.

这是一个非常简单的示例,但总的来说,范围可以成为简化您工作的非常强大的工具。

Check out this link: API Description

查看此链接:API 说明

回答by Akshatha

scope in active record is like class methods but they return Relation object which means you can call another scope or active record querying method on it.

活动记录中的范围类似于类方法,但它们返回 Relation 对象,这意味着您可以在其上调用另一个范围或活动记录查询方法。

For example, if you have a Zombie model (zombies table) with below mentioned scope methods,

例如,如果您有一个带有下面提到的范围方法的僵尸模型(僵尸表),

class Zombie
  scope :rotting, -> { where(rotting: true) }
  scope :fresh, -> { where('age < ?', 25) }
  scope :recent, -> { order(created_at: :desc) }
end

And you call

你打电话

Zombie.rotting.fresh.recent.limit(3)

It translates to the below in SQL,

它在 SQL 中转换为以下内容,

select "zombies.*" from "zombies" where "zombies"."rotting" = 't' and (age<20) order by create_at desc limit 3

Example above is based on rails 4 syntax

上面的例子基于 rails 4 语法

回答by Imran Ahmad

Scopes are nothing but class methods.

作用域只不过是类方法。

Why use them?

为什么要使用它们?

Scoping allows you to specify commonly-used queries(it can be considered as a shortcut for long or most frequently used queries) which can be referenced as method calls on the association objects or models. With these scopes, you can use every method previously covered such as where, joins and includes. All scope methods will return an ActiveRecord::Relation objectwhich will allow for further methods (such as other scopes) to be called on it.

作用域允许您指定常用的查询(可以将其视为长查询或最常用查询的快捷方式),这些查询可以作为关联对象或模型上的方法调用进行引用。通过这些范围,您可以使用之前介绍的所有方法,例如 where、joins 和 includes。所有范围方法都将返回一个 ActiveRecord::Relation 对象,该对象将允许在其上调用更多方法(例如其他范围)。

To define a simple scope, we use the scope method inside the class, passing the query that we'd like to run when this scope is called:

要定义一个简单的范围,我们在类中使用 scope 方法,传递我们希望在调用此范围时运行的查询:

class Article < ActiveRecord::Base
  scope :published, -> { where(published: true) }
end

This is exactly the same as defining a class method, and which you use is a matter of personal preference:

这与定义类方法完全相同,您使用哪种方法取决于个人喜好:

class Article < ActiveRecord::Base
  def self.published
    where(published: true)
  end
end

Please follow the following link for full description with example. I hope this will help you.

请按照以下链接进行完整说明和示例。我希望这能帮到您。

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

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

回答by 123

The best way to understand about the details is to go to API Documentation.

了解详细信息的最佳方式是访问 API 文档。

You'll get the complete details and the ways we can use Scopes.

您将获得完整的详细信息以及我们使用 Scopes 的方式。

API Documentation of Scope

范围的 API 文档

回答by BKSpurgeon

  • Imagine you have a model: Person.
  • 假设您有一个模型:Person

Now imagine you :

现在想象你:

  • want all the people in the world who have red hair.
  • want all the people in the world who play cricket
  • 想要世界上所有红头发的人。
  • 想要世界上所有打板球的人

You could get those particular classes of people by using a scope!

您可以通过使用范围来获取那些特定类别的人!

Person.red_hair.cricket ## finds all people with red hair who play cricket
Person.red_hair ## finds all people with red hair
Person.cricket ## finds all people who play cricket.

Now that wasn't so hard was it?

现在那不是那么难吗?