识别用户的重复多封邮件。

问题描述 投票:0回答:0

我有User和UserEmails模型。

用户列:id,first_name,last_nameUserEmails:id,user_id,email,primary。

我需要一个这样的函数(https:/imgur.coma762iK9r)

其实质是:通过或邮件识别重复用户。

例如:用户1。

{ id: 1, first_name: 'Test', last_name: 'User1' }

用户1的用户电子邮件。

{ email: '[email protected]', primary: true, user_id: 1 }

用户2:用户2的用户邮件。

{ id: 2, first_name: 'Test', last_name: 'User2' }

用户2的用户邮件。

{ { email: '[email protected]', primary: true, user_id: 2 }, { email: '[email protected]', primary: false, user_id: 2 }, { email: '[email protected]', primary: false, user_id: 2 } }

用户3:用户3的UserEmails

{ id: 3, first_name: 'Test', last_name: 'User3' }

用户3的用户邮件:

{ { email: '[email protected]', primary: true, user_id: 3 }, { email: '[email protected]', primary: false, user_id: 3 }, { email: '[email protected]', primary: false, user_id: 3 } }

我需要一个查询来查找重复的内容,并返回一个ID数组,像这样。[[1, 2, 3], [...]]

我有一个查询,但它错误地显示了一个ID数组。

ids = User.left_outer_joins(:user_emails)
          .select('array_agg(users.id) AS ids')
          .where('user_emails.email IS NOT NULL')             
          .group("user_emails.email")
          .having('COUNT(1) > 1')
          .map(&:ids)

我得到了这个数组,但是这个数组 [2, 3] 已经在数组中 [1,2,3].

[[1, 2, 3], [2, 3]]

在显示用户的邮件重复的情况下,我如何对不会重复的数组id进行分组或排序?

ruby-on-rails postgresql ruby-on-rails-5
© www.soinside.com 2019 - 2024. All rights reserved.