I have a hash like so:
[
{
:lname => "Brown",
:email => "james@intuit.com",
:fname => "James"
},
{
:lname => nil,
:email => "brad@intuit.com",
:fname => nil
},
{
:lname => "Smith",
:email => "brad@intuit.com",
:fname => "Brad"
},
{
:lname => nil,
:email => "brad@intuit.com",
:fname => nil
},
{
:lname => "Smith",
:email => "brad@intuit.com",
:fname => "Brad"
},
{
:lname => nil,
:email => "brad@intuit.com",
:fname => nil
}
]
What I would like to learn how to do is how to remove a record if it is duplicate. Meaning, see how there are several “brad@intuit.com” how can I remove the duplicate records, meaning remove all the others that have an email of “brad@intuit.com”…. Making email the key not the other fields?
I know this is an old thread, but Rails has a method on ‘Enumerable’ called ‘index_by’ which can be handy in this case:
Now you can get the unique rows as follows:
To merge the rows with the same email id.
Custom but efficient method: