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 795701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:31:17+00:00 2026-05-14T22:31:17+00:00

I have a model called HeroStatus with the following attributes: id user_id recordable_type hero_type

  • 0

I have a model called HeroStatus with the following attributes:

  • id
  • user_id
  • recordable_type
  • hero_type (can be NULL!)
  • recordable_id
  • created_at

There are over 100 hero_statuses, and a user can have many hero_statuses, but can’t have the same hero_status more than once.

A user’s hero_status is uniquely identified by the combination of recordable_type + hero_type + recordable_id. What I’m trying to say essentially is that there can’t be a duplicate hero_status for a specific user.

Unfortunately, I didn’t have a validation in place to assure this, so I got some duplicate hero_statuses for users after I made some code changes. For example:

user_id = 18
recordable_type = 'Evil'
hero_type = 'Halitosis'
recordable_id = 1
created_at = '2010-05-03 18:30:30'

user_id = 18
recordable_type = 'Evil'
hero_type = 'Halitosis'
recordable_id = 1
created_at = '2009-03-03 15:30:00'

user_id = 18
recordable_type = 'Good'
hero_type = 'Hugs'
recordable_id = 1
created_at = '2009-02-03 12:30:00'

user_id = 18
recordable_type = 'Good'
hero_type = NULL
recordable_id = 2
created_at = '2009-012-03 08:30:00'

(Last two are not a dups obviously. First two are.) So what I want to do is get rid of the duplicate hero_status. Which one? The one with the most-recent date.

I have three questions:

  1. How do I remove the duplicates using a SQL-only approach?

  2. How do I remove the duplicates using a pure Ruby solution? Something similar to this: Removing "duplicate objects".

  3. How do I put a validation in place to prevent duplicate entries in the future?

  • 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-14T22:31:17+00:00Added an answer on May 14, 2026 at 10:31 pm

    For an SQL only approach, I would use this query – (I’m assuming the id’s are unique.)

    DELETE FROM HeroStatus WHERE id IN
    (SELECT id FROM 
       (SELECT user_id, recordable_type, hero_type, recordable_id, MAX(created_at)
         GROUP BY del.user_id, recordable_type, hero_type, recordable_id
         HAVING Count(id)>1) AS del 
          INNER JOIN HeroStatus AS hs ON
          hs.user_id=del.user_id AND hs.recordable_type=del.recordable_type 
           AND hs.hero_type=del.hero_type AND hs.recordable_id=del.recordable_id 
           AND hs.created_at = del.created_at)
    

    A bit of a monster! The query finds all duplicates using the natural key (user_id, recordable_type, hero_type) and selects the one with the largest created_at value (most recently created). It then finds the IDs of those rows (by joining back to the main table) and deletes rows with that id.

    (Please try this on a copy of the table first and verify you get the results you want! 🙂

    To prevent this happening in future, add a unique index or constraint over the columns user_id, recordable_type, hero_type, recordable_id. E.g.

    ALTER TABLE HeroStatus 
    ADD UNIQUE (user_id, recordable_type, hero_type, recordable_id)
    

    EDIT:

    You add (and remove) this index within a migration like this:

    add_index(:HeroStatus, [:user_id, :recordable_type, :hero_type, :recordable_id], :unique => true)
    remove_index(:HeroStatus, :column => [:user_id, :recordable_type, :hero_type, :recordable_id], :unique => true)
    

    Or, if you want to explicitly name it:

    add_index(:HeroStatus, [:user_id, :recordable_type, :hero_type, :recordable_id], :unique => true, :name => :my_unique_index)
    remove_index(:HeroStatus, :name => :my_unique_index)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model called Profile which is belong_to User, so there is 'user_id'
Say I have a model called User that has the following parameters: favorite_color, favorite_animal,
I have model called test, and test can have many tests, and should be
I have a model called book and in that model there is a foreignkey
I have a model called Contacts. Contacts can have different status bad, positive, wrong...
I have a model called Company, and in the Show view have the following:
I have a model called 'BusinessPage' which can be associated with 1 or more
I have a model called Tournament, which has_many Entries. There is a method in
Let's say I have a model called Theme, which has several attributes setting interface
I have a model called Contact which has_one guest like so. class Contact <

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.