On Thu, Aug 14, 2008 at 12:27 PM, Erwin <yves_dufour / mac.com> wrote:
> On 14 août, 19:24, Erwin <yves_duf... / mac.com> wrote:
>> 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
>
> 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 ?

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