I was trying to write a method in ruby that could:
- Query through and find all of the duplicates in our DB that meet a certain criteria (first
name,last_name,account_id, andtitleall match) - Iterate through them and combine the contents of a field called
possible_unique_keysfor all duplicates - Remove the duplicate records now that we have 1 record for each with the concatenated field
I started with a quick query to pull out only the dup candidates:
results = ActiveRecord::Base.connection.select_all( "SELECT id, first_name, last_name, title, account_id, possible_unique_keys, device_contacts_count FROM CONTACTS AS a WHERE 1 < (SELECT Count(*) FROM CONTACTS AS b WHERE b.first_name = a.first_name AND b.last_name = a.last_name AND b.title = a.title AND b.account_id = a.account_id)" )
It pulls out what appears to be the correct results set with the fields that I need. The first problem comes when I try to iterate through them:
results.each do |contact|
contact.first_name
end
I’m really just getting started trying to get my head around this problem but I get an error when I try to reference any of the fields as I did above when trying to reference the first_name:
NoMethodError: undefined method
first_namefor#<Hash:0x00000006641cb0>
Returns an array of record hashes with the column names as keys and column values as values.
or