开发者

acts_as_taggable_on with a collection

开发者 https://www.devze.com 2023-02-05 09:47 出处:网络
I have a list of products, which belong to a category.Each product has tags. See following example (psuedocode)

I have a list of products, which belong to a category. Each product has tags. See following example (psuedocode)

Category = transport
Products = car, train, bus

car has tags = small, fast
train has tags = fast, large
bus has tags = slow, large

How can I list all tags from products that are in the transport category? the result should be ["small", "fast", 开发者_开发技巧"large", "slow"]


Define an array to hold the tags from products. Iterate over the products that belong to the Category. I am assuming, you have relationships set. Remove duplicates from the array, if any.

@tags = [] 
@category.products.each { |p| @tags << p.tags }
@tags.uniq!
0

精彩评论

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

关注公众号