如何从用户数组中提取电子邮件 [英] how to pluck email from array of users

查看:28
本文介绍了如何从用户数组中提取电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我执行 User.all.pluck(:email) 那么它就可以正常工作.

If I do User.all.pluck(:email) then it works fine.

但是如果我这样做

arr = Array.new
arr = User.all

然后

arr.pluck(:email)

这是引发以下错误

undefined method `pluck' for #<Array:0x007f4ff8daf3c8>

这意味着我不能将 pluck 与数组一起使用,那么我们如何才能像上面那样在一行中从一组记录中获取特定的字段值.我不想遍历数组中的每条记录.

which means I cannot use pluck with arrays, so how can we get particular field value from an array of records in just one line like above. I don't want to loop through each record in an array.

推荐答案

pluck 对于进行极简的 db 查询很有用.

pluck is useful to do a minimalist db query.

当你有一个数组时,只需使用map:

When you have an array, just use map:

arr.map(&:email)

这篇关于如何从用户数组中提取电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆