Ruby on rails Rails中消除重复人员记录的最佳方法
我正在编写一个rails应用程序,其人物模型如下:Ruby on rails Rails中消除重复人员记录的最佳方法,ruby-on-rails,duplicates,duplicate-data,Ruby On Rails,Duplicates,Duplicate Data,我正在编写一个rails应用程序,其人物模型如下: create_table "people", :force => true do |t| t.string "first_name" t.string "last_name" t.string "email" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false
create_table "people", :force => true do |t|
t.string "first_name"
t.string "last_name"
t.string "email"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
# This isn't particularly efficient, but it should return an array in which
# each element is a list of duplicated people (assuming we define duplicates
# by doubled email addresses).
def self.find_duplicates
array = []
self.each do |person|
similar = self.find_by_email
if similar.count > 1
array << similar
end
end
return array
end
我有以下两个步骤:
在第2步之后,我知道如何发现这两条记录是重复的(即同一个人),我的问题是如何在模型中表示这两条记录?我是否应该添加一个“replicate_of_person_id”类型的字段,并将该字段中第一条记录的id放在第二条记录中?有更好的方法吗?不是100%确定你想要什么。如果您只想查找重复项,并在数组中列出它们,可以创建如下方法:
create_table "people", :force => true do |t|
t.string "first_name"
t.string "last_name"
t.string "email"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
# This isn't particularly efficient, but it should return an array in which
# each element is a list of duplicated people (assuming we define duplicates
# by doubled email addresses).
def self.find_duplicates
array = []
self.each do |person|
similar = self.find_by_email
if similar.count > 1
array << similar
end
end
return array
end
不过,在此之前,要确保所有电子邮件都在同一个案例中。您也可以在模型中执行类似操作:
before_validation :format_emails
def format_emails
self.email = self.email.downcase
end
您可以将所有记录链接在一起。想到的第一个方案是保持id最低的记录为赢家,并让所有被骗者指向它。你也可以做一个has_和wou belies_to_many,这将涉及一个单独的表,其中每个记录都表明这两个人是相同的。然而,后者随着人数的增加呈二次增长
或者,将第二个文件中的所有信息复制到第一个文件中,然后删除第二个文件 应在数据库上执行唯一性检查,以避免线程化和漂移问题,删除处理这些问题的复杂代码并提高效率。线程化:应用程序的两个实例同时发送相同的数据。应用程序检查无法捕捉到这一点(我想也可以称为
竞争条件)。漂移:主从数据库服务器设置,从数据库服务器未收到新数据集,get询问数据是否存在,并如实回答否。然后将复制数据集。抱歉,应该更清楚。在第2步之后,我知道如何发现这两条记录是重复的(即同一个人),我的问题是如何在模型中表示这两条记录?我是否应该添加一个“replicate_of_person_id”类型的字段,并将该字段中第一条记录的id放在第二条记录中?