sutee sutee - 1 year ago 139
Ruby Question

Uniq by object attribute in Ruby

What's the most elegant way to select out objects in an array that are unique with respect to one or more attributes?

These objects are stored in ActiveRecord so using AR's methods would be fine too.

Answer Source

Use Array#uniq with a block:

@photos = @photos.uniq { |p| p.album_id }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download