I get an array with person
objects. A person
can have several houses
:
person.houses => ActiveRecord_Associations_CollectionProxy
Some houses can belong to several persons
. In my code I would like to get all unique houses
for the persons that are in the Array.
This is a long version of the code:
def persons_houses(persons)
unique_houses = []
persons.each do |person|
person.houses.each do |house|
unique_houses << house if !unique_houses.include? house
end
end
unique_houses
end
Do you know a shorter code for this persons_houses(persons)
method?
I was thinking of mapping the persons houses, concat them at the same time and then return unique values
Something like: (But this is not valid Ruby)
persons.map { |person| concat(person.houses) }.uniq
Thanks for your help!
Aucun commentaire:
Enregistrer un commentaire