简体   繁体   English

滑轨从模型中选择

[英]Rails select from model

in some video i saw next string: 在一些视频中,我看到了下一个字符串:

User.select(:email).map(&:email)

tell me plz what does it means 告诉我这是什么意思

i know that string 我知道那串

User.select(:email)

is selecting only the email column from the db, but i don't understand what means 从数据库中仅选择电子邮件列,但我不明白这是什么意思

.map(&:email)

and can we change User.select(:email) to User.pluck(:email) 我们可以将User.select(:email)更改为User.pluck(:email)

because from tutorial i understand thats the same methods. 因为从教程中我知道多数民众赞成在相同的方法。 is this true? 这是真的?

The map(&:email) maps your array of User s to a map of string containing only the users' emails. map(&:email)将您的User数组映射到仅包含用户电子邮件的字符串映射。

The Array.map iterates over your current array, and creates a new one, by calling the parameter block, and storing the result in the new array. Array.map遍历当前数组,并通过调用参数块并将结果存储在新数组中来创建一个新数组。 It is equivalent with this: 与此等效:

new_array = []
Users.select(:email).each do |user|
  new_array << user.email
end

User.select(:email)

is returning an array of User objects. 返回一个User对象数组。 The expression 表达方式

User.select(:email).map(&:email)

selects the email attribute of that objects only. 仅选择该对象的电子邮件属性。 So you end up with an array of email strings. 因此,您最终得到了一系列电子邮件字符串。 That's at the end the same as 最后与

User.pluck(:email)

But it's is different from User.select(:email) for that reason. 但由于这个原因,它与User.select(:email)不同。

See the documentation of pluck als well. 很好地查看弹拨文件

I suppose you already know what map(&:email) gives you, I assume you are asking how and why because it was the same thing that struck me when I first saw this. 我想你已经知道map(&:email)给了你什么,我想你是在问为什么和为什么,因为当我第一次看到它的时候,那是同一件事。 So this is one of the more advanced ruby magic that voodoo's results back to you :) 因此,这是伏都教将结果反馈给您的更高级的红宝石魔术之一:)

Basically lets look at the map function, in itself the most basic usage is to accept a block level command. 基本上让我们看一下map函数,其本身最基本的用法就是接受一个块级命令。 And after iterating through, takes the default return value and chuck it into an array for you usage. 遍历之后,获取默认的返回值并将其放入数组以供使用。 For example, lets see this 例如,让我们看一下

list = User.all 

so we get a list of user objects [User model,User model] etc. 因此我们获得了用户对象的列表[User model,User model]等。

list.map do |user|
  user.email
end

if u run this block in IRB or Rails Console, you get ["some@email.here, another@email.here"] etc. so lets catch this result and assign it into a variable 如果您在IRB或Rails控制台中运行此块,则会得到[“ some@email.here,another@email.here”]等,因此让我们捕获此结果并将其分配给变量

email_list = list.map do |user|
  user.email
end

now email_list should equal to ["some@email.here, another@email.here"] Now that you get the background to the map function, lets delve into the various ways it can accept a parameter 现在email_list应该等于[“ some@email.here,another@email.here”]现在,您已经获得了map函数的背景,让我们深入研究一下它可以接受参数的各种方式

list.map {|user| user.email }

this is the same as the above, but using curly braces to enclose the block logic 与上面的相同,但是使用花括号将块逻辑括起来

list.map(&:email)

this is the shorthand for the above, by defining the block for you, you just supply what child function you wish to run on the block item. 这是上面的简写,通过为您定义块,您只需提供希望在块项目上运行的子功能即可。

Hope this has given you alittle insight into short hand methods and its block level equivalents. 希望这能使您对速记方法及其块级等效项有一定的了解。

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

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