开发者

Help with creating an array

开发者 https://www.devze.com 2023-03-29 14:55 出处:网络
I have a categories model. attr_accessible :nam开发者_如何学Goe, :parent, :tags, :active, :image

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消