Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3611552
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:53:23+00:00 2026-05-18T21:53:23+00:00

I have users which has first_name and last_name fields and i need to do

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:53:24+00:00Added an answer on May 18, 2026 at 9:53 pm

    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" )
    

    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">]
    

    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">]
    

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

    Edit:

    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")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table of users which has a username column consisting of a
I need to develop a CRM system which will allow users to have a
I have a table User which has an identity column UserID , now what
I have user control named DateTimeUC which has two textboxes on its markup: <asp:TextBox
I have an application which had optional extras depending on if the user has
I am programming a website in which users will have a number of settings,
Our users have created MS-Excel spreadsheets which over time have evolved into fairly complex
I have an application in which users interact with each-other. I want to visualize
I have a datagridview where the users can select which subset of columns to
I have a script which will be run interactively by non-technical users. The script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.