查找所有具有重复名称的用户 [英] Finding all the users that have duplicate names

查看:137
本文介绍了查找所有具有重复名称的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用户有FIRST_NAME和LAST_NAME领域,我需要做的红宝石查找具有重复帐户基于姓和名的用户。比如我想有一个发现,将搜遍所有的其他用户,并查找是否有相同的名字和电子邮件。我在想一个嵌套的循环像这样

I have users which has first_name and last_name fields and i need to do a ruby find all the users that have duplicate accounts based on first and last names. For example i want to have a find that will search through all the other users and find if any have the same name and email. I was thinking a nested loop like this

User.all.each do |user|
 //maybe another loop to search through all the users and maybe if a match occurs put that user in an array
end

有没有更好的办法

Is there a better way

推荐答案

您可以去朝着缩小通过找出重复的数据是摆在首位你的搜索很长的路要走。例如,假设你想找到的是多次使用的名字和电子邮件的每个组合。

You could go a long way toward narrowing down your search by finding out what the duplicated data is in the first place. For example, say you want to find each combination of first name and email that is used more than once.

User.find(:all, :group => [:first, :email], :having => "count(*) > 1" )

这将返回包含每个重复记录中的一个阵列。从这一点,说返回的用户之一,有弗雷德和fred@example.com,那么你可以搜索具有这些值来找到所有受影响的用户的唯一用户。

That will return an array containing one of each of the duplicated records. From that, say one of the returned users had "Fred" and "fred@example.com" then you could search for only Users having those values to find all of the affected users.

从返回的查找会像下面这样。请注意,数组只包含从每组重复用户的一条记录。

The return from that find will be something like the following. Note that the array only contains a single record from each set of duplicated users.

[#<User id: 3, first: "foo", last: "barney", email: "foo@example.com", created_at: "2010-12-30 17:14:43", updated_at: "2010-12-30 17:14:43">, 
 #<User id: 5, first: "foo1", last: "baasdasdr", email: "abc@example.com", created_at: "2010-12-30 17:20:49", updated_at: "2010-12-30 17:20:49">]

例如,在该数组的第一个元素表示一个用户与foo和foo@example.com。他们的休息可以根据需要用一个查找被拉动的数据库的出

For example, the first element in that array shows one user with "foo" and "foo@example.com". The rest of them can be pulled out of the database as needed with a find.

> User.find(:all, :conditions => {:email => "foo@example.com", :first => "foo"})
 => [#<User id: 1, first: "foo", last: "bar", email: "foo@example.com", created_at: "2010-12-30 17:14:28", updated_at: "2010-12-30 17:14:28">, 
     #<User id: 3, first: "foo", last: "barney", email: "foo@example.com", created_at: "2010-12-30 17:14:43", updated_at: "2010-12-30 17:14:43">]

和它也好像你会想一些更好的验证添加到您的code至prevent重复的未来。

And it also seems like you'll want to add some better validation to your code to prevent duplicates in the future.

编辑:

如果你需要使用的find_by_sql 的大铁锤,由于Rails 2.2和更早版本不支持:有查找,以下应该工作,并给你,我的上述相同的数组。

If you need to use the big hammer of find_by_sql, because Rails 2.2 and earlier didn't support :having with find, the following should work and give you the same array that I described above.

User.find_by_sql("select * from users group by first,email having count(*) > 1")

这篇关于查找所有具有重复名称的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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