Ruby-on-rails Rails 中的 pluck 和 collect 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12176102/
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-02 20:41:50  来源:igfitidea点击:

What is the difference between pluck and collect in Rails?

ruby-on-railsrubyrails-activerecord

提问by Mohit Jain

Here are two sample codes.

这是两个示例代码。

First one with collect:

第一个collect

User.first.gifts.collect(&:id)

Second one with pluck:

第二个pluck

User.first.gifts.pluck(:id)

Is there any difference between them in performance or something else?

它们在性能或其他方面有什么区别吗?

回答by apneadiving

pluckis on the db level. It will only query the particular field. See this.

pluck是在数据库级别。它只会查询特定的字段。看到这个

When you do:

当你这样做时:

 User.first.gifts.collect(&:id)

You have objects with all fields loaded and you simply get the idthanks to the method based on Enumerable.

您已经加载了所有字段的对象,并且您只需id感谢基于 Enumerable 的方法。

So:

所以:

  • if you onlyneed the idwith Rails 4, use ids: User.first.gifts.ids

  • if you onlyneed some fields with Rails 4, use pluck: User.first.gifts.pluck(:id, :name, ...)

  • if you onlyneed one field with Rails 3, use pluck: User.first.gifts.pluck(:id)

  • if you need allfields, use collect

  • if you need some fields with Rails 4, still use pluck

  • if you need some fields with Rails 3, use selectand collect

  • 如果您需要id使用 Rails 4,请使用idsUser.first.gifts.ids

  • 如果您需要 Rails 4 的某些字段,请使用pluckUser.first.gifts.pluck(:id, :name, ...)

  • 如果您需要 Rails 3 的一个字段,请使用pluckUser.first.gifts.pluck(:id)

  • 如果您需要所有字段,请使用collect

  • 如果你需要一些 Rails 4 的字段,仍然使用 pluck

  • 如果您需要 Rails 3 的一些字段,请使用selectcollect

回答by Fellow Stranger

Yes. According to Rails guides, pluckdirectly converts a database result into an array, without constructing ActiveRecordobjects. This means better performance for a large or often-running query.

是的。根据 Rails 指南pluck直接将数据库结果转换为array,无需构造ActiveRecord对象。这意味着大型或经常运行的查询具有更好的性能。

In addition to @apneadiving's answer, pluckcan take both single and multiple column names as argument:

除了@apneadiving 的答案之外,pluck还可以将单个和多个列名作为参数:

Client.pluck(:id, :name)
# SELECT clients.id, clients.name FROM clients
# => [[1, 'David'], [2, 'Jeremy'], [3, 'Jose']]

回答by Thorin

The basic and main difference is that Pluck applies on db level and collect get all data and then return record to you when you need all records use collect and when few fields then use pluck

基本和主要区别在于 Pluck 适用于 db 级别并收集获取所有数据,然后在您需要所有记录时使用收集并将记录返回给您,而当字段很少时则使用 pluck

回答by Chimed Palden

If there is a case where you are using few attributes of the retrieved record. In such cases you should use pluck.

如果在某些情况下您使用的检索记录的属性很少。在这种情况下,您应该使用pluck.

User.collect(&:email)

In above example if you only need email attribute than you are wasting memory and time. Because it will retrieve all the columns from user table in the database, allocates the memory for each attributes (including the attributes which you will never use)

在上面的例子中,如果你只需要 email 属性而不是浪费内存和时间。因为它将检索数据库中用户表中的所有列,为每个属性(包括您永远不会使用的属性)分配内存

NOTE: pluckdoes not return ActiveRecord_Relation of the user

注意:pluck不返回用户的 ActiveRecord_Relation