I'm using RoR 5. How do I select unique attribute values from an array of objects? I've tried
attr_values = my_objects.uniq{|r| r.attr_name}
But "attr_values" is an array of objects of the same type that "my_objects" consists of. I want the array to be objects of the type of "r.attr_name."
.distinctor grouping instead which avoids loading a huge amount of records from the DB.my_objects.pluck('DISTINCT attr_name')could be better than pulling all the data out in the first place.attr_values = my_objects.map(&:attr_name).uniqis what he is looking for but its a really fuzzy question. @SergioTulentsev.distinctand.pluckwithout the SQL segment, see my answer. @bkunzi01, your solution creates a big array, only to remove duplicates later, it won't be performant.