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

  • SEARCH
  • Home
  • 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 9141131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:36:20+00:00 2026-06-17T09:36:20+00:00

I’m working on a Rails app (Ruby 1.9.2 / Rails 3.0.3) that keeps track

  • 0

I’m working on a Rails app (Ruby 1.9.2 / Rails 3.0.3) that keeps track of people and their memberships to different teams over time. I’m having trouble coming up with a scalable way to combine duplicate Person objects. By ‘combine’ I mean to delete all but one of the duplicate Person objects and update all references to point to the remaining copy of that Person. Here’s some code:

Models:

Person.rb

class Person < ActiveRecord::Base
  has_many :rostered_people, :dependent => :destroy
  has_many :rosters, :through => :rostered_people
  has_many :crews, :through => :rosters

    def crew(year = Time.now.year)
      all_rosters = RosteredPerson.find_all_by_person_id(id).collect {|t| t.roster_id}
      r = Roster.find_by_id_and_year(all_rosters, year)
      r and r.crew
    end
end

Crew.rb

class Crew < ActiveRecord::Base
  has_many :rosters
  has_many :people, :through => :rosters
end

Roster.rb

class Roster < ActiveRecord::Base
  has_many :rostered_people, :dependent => :destroy
  has_many :people, :through => :rostered_people
  belongs_to :crew
end

RosteredPerson.rb

class RosteredPerson < ActiveRecord::Base
  belongs_to :roster
  belongs_to :person
end

Person objects can be created with just a first and last name, but they have one truly unique field called iqcs_num (think of it like a social security number) which can be optionally stored on either the create or update actions.

So within the create and update actions, I would like to implement a check for duplicate Person objects, delete the duplicates, then update all of the crew and roster references to point to the remaining Person.

Would it be safe to use .update_all on each model? That seems kind of brute force, especially since I will probably add more models in the future that depend on Person and I don’t want to have to remember to maintain the find_duplicate function.

Thanks for the help!

  • 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-06-17T09:36:21+00:00Added an answer on June 17, 2026 at 9:36 am

    The ‘scalable’ way to deal with this is to make the de-duplication process part of the app’s normal function – whenever you save a record, make sure it’s not a duplicate. You can do this by adding a callback to the Person model. Perhaps something like this:

    before_save :check_for_duplicate
    
    def check_for_duplicate
      if iqcs_num
        dup = Person.find_by_iqcs_num(self.iqcs_num)
        if dup && dup.id != self.id
          # move associated objects to existing record
          dup.crews = dup.crews + self.crews
    
          # update existing record
          dup.update_attributes(:name => self.name, :other_field => self.other_field)
    
          # delete this record
          self.destroy
    
          # return false, so that no other callbacks get triggered
          return false
        end
      end
    end
    

    You’ll want to make sure that you index the table you store Person objects in on the iqcs_num column, so that this lookup stays efficient as the number of records grows – it’s going to be performed every time you update a Person record, after all.

    I don’t know that you can get out of keeping the callback up-to-date – it’s entirely likely that different sorts of associated objects will have to be moved differently. On the other hand, it only exists in one place, and it’s the same place you’d be adding the associations anyway – in the model.

    Finally, to make sure your code is working, you’ll probably want to add a validation on the Person model that prevents duplicates from existing. Something like:

    validates :iqcs_num, :uniqueness => true, :allow_nil => true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I've tracked down a weird MySQL problem to the two different ways I was

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.