[英]Comparing one array against another in rails
I am using the 'contacts' gem in rails to retrieve a users contacts from their mail application. 我正在使用Rails中的“联系人” gem从其邮件应用程序中检索用户联系人。 It returns the contacts like so:
它返回联系人,如下所示:
["person name", "personemail@example.com"], ["person name", "personemail@example.com"], ["person name", "personemail@example.com"] etc...
I want to compare this list to the Users already signed up for my site 我想将此列表与已经为我的网站注册的用户进行比较
Users.find(:all) returns: Users.find(:all)返回:
[#<User id: 11, login: "examplelogin", email: "example@example.com">, #<User id: 12, login: "examplelogin", email: "example@example.com">, etc... ]
What is the best way to go about comparing the gmail contact emails to the User emails and displaying only the ones that are a match? 比较gmail联系人电子邮件和用户电子邮件并仅显示匹配的电子邮件的最佳方法是什么?
I was thinking something like: 我想的是:
@contacts = Contacts::Gmail[params[:from]].new(params[:login], params[:password]).contacts
@contacts.each do |c|
@email = c[1]
@user = Users.find_by_email(@email)
end
Which would presumably only return the users where there was a match. 大概只会将匹配的用户返回。 I feel like there must be a better way to go about this that I am not considering.
我觉得必须有一个我没有考虑的更好的方法来解决这个问题。 Any suggestions?
有什么建议么?
@users = Users.find_all_by_email(@contacts.collect{|contact| contact[1]})
edit: 编辑:
What you had before would perform a find for each contact and leave you only with the result of the last query, either a user or not depending on whether one existed with this email. 您之前拥有的内容将为每个联系人执行查找,而只留下最后一个查询的结果,取决于用户是否使用此电子邮件,该查询是否为用户。 This performs one query and returns an array of all successful matches:
这将执行一个查询并返回所有成功匹配项的数组:
where 'users'.'email' in array_of_email_addresses.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.