Nested loop

I know there is DRY way tow rite that, but I don’t remember how

residence_rentals_ids = []
residences = user.franchise.residences
for residence in residences
residence_rentals_ids = residence.rentals.map {|rental|
rental[:id] }.compact
end

I tried unsuccessfully :

residence_rentals_ids = user.franchise.residences.each { |residence|
residence.rentals.map { |rental| rental[:id}.compact }

thanks for your lights

erwin

On 14 août, 19:24, Erwin [email protected] wrote:

residence_rentals_ids = user.franchise.residences.each { |residence|
residence.rentals.map { |rental| rental[:id}.compact }

thanks for your lights

erwin

got it :
residence_rentals_ids = []
user.franchise.residences.each { |residence| residence.rentals.map { |
rental| residence_rentals_ids << rental[:id] }.compact }

but is there a way to avoid the residence_rentals_ids = [] line,
including the array initialization into a block ?

On 8/14/08, Erwin [email protected] wrote:

residence_rentals_ids = []
user.franchise.residences.each { |residence| residence.rentals.map { |
rental| residence_rentals_ids << rental[:id] }.compact }

but is there a way to avoid the residence_rentals_ids = [] line,
including the array initialization into a block ?

residence_rentals_ids = user.franchise.residences.map{ |residence|
residence.rentals.map { |rental| rental[:id] }.compact }.flatten

On Thu, Aug 14, 2008 at 12:27 PM, Erwin [email protected] wrote:

I tried unsuccessfully :
user.franchise.residences.each { |residence| residence.rentals.map { |
rental| residence_rentals_ids << rental[:id] }.compact }

but is there a way to avoid the residence_rentals_ids = [] line,
including the array initialization into a block ?

Generally you don’t want a map without assigning the result to
something.

Does this do what you want?

residence_rental_ids = user.franchise.residences.map{|residence|
residence.rentals.map{|rental| rental[:id] } }.flatten

-Michael

On 14 août, 20:33, Michael L. [email protected] wrote:

residence_rentals_ids = []
residence_rental_ids = user.franchise.residences.map{|residence|
residence.rentals.map{|rental| rental[:id] } }.flatten

-Michael

that’s it… thanks a lot… I was missing the .flatten functionality