Ruby/Rails: Merge result of map
-
21-12-2019 - |
Question
I have @products
collection and I want to merge image_ids
to single array. Here is the output of @products.map(&:image_ids)
:
@products.map(&:image_ids)
=> [[1654, 1655, 1656],
[1649, 1650, 1652, 1653],
[1625],
[1624],
[1623],
[1622],
[1621],
[1620],
[1619],
[1618]]
Solution
Do as below using flat_map
:
@products.flat_map(&:image_ids)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow