kakubei kakubei - 2 months ago 5
Ruby Question

Ruby remove duplicate entries in array of hashes but based on more than one value

I've seen numerous questions about this but only with one key, never for multiple keys.

I have the following array of hashes:

a = [{:name=>"Yes, Yes, Yes", :artist=>"Some Dude", :composer=> 'First Dude', :duration=>"3:21"},
{:name=>"Chick on the Side", :artist=>"Another Dude", :duration=>"3:20"},
{:name=>"Luv Is", :duration=>"3:13"},
{:name=>"Yes, Yes, Yes", :artist=>"Some Dude", :composer=> 'First Dude', :duration=>"2"},
{:name=>"Chick on the Side", :artist=>"Another Dude"}]

won't work here because the duration is different or might not even exist. I have a unique key set up in the database that does not allow duplicate entries by the same name, artist and composer so I sometimes get errors when people have duplicate entries for these 3 keys.

Is there a way to run
that would check for those 3 keys?
I tried a block like this:

new_tracks.uniq do |a_track|

But that ignores anything where the key is not present (any entry without a composer does not meet the above criteria for example).

I could always use just the
key but that would mean I'm getting rid of potentially valid tracks in compilations that have the same title but different artist or composer.

This is with Ruby 2.0.


uniq accepts a block. If a block is given, it will use the return value of the block for comparison.

Your code was close to the solution, but in your code the return value was only a_track[:composer] which is the last evaluated statement.

You can join the attributes you want into a string and return that string.

new_tracks.uniq { |track| [track[:name], track[:artist], track[:composer]].join(":") }

A possible refactoring is

new_tracks.uniq { |track| track.attributes.slice('name', 'artist', 'composer').values.join(":") }

Or add a custom method in your model that performs the join, and call it

class Track < ActiveRecord::Base
  def digest
    attributes.slice('name', 'artist', 'composer').values.join(":")