Help with creating an array
I have a categories model.
attr_accessible :nam开发者_如何学Goe, :parent, :tags, :active, :image
Sample data will be something like
"Bakery",null,"bakehouse, baker, bakeries, bakers, bakery, bakeshop, biscuit factory, boulangerie, bread bakery, bread, cake bakery, cake topper, cake toppers, cake, cakes, confectionery, cook shop, pastry kitchen, pastry shop, pastry, patisserie, wedding cake", 1, null
"Beauty & Makeup",null,"makeup, ", 1, null
Doing @categories = Category.all gives me all categories with all the fields, now I want to create an array with the tags and the category name. The array data should be like
[["Bakery", "bakehouse"]
["Bakery", "baker"]
["Bakery", "bakeries"]
["Bakery", "bakers"]
["Bakery", "bakery"]
.....
["Beauty & Makeup", "makeup"]]
how can this be done?
@categories.map {|cat| cat.tags.split(/, /).map {|tag| [cat.name, tag]}}
should do the trick.
精彩评论