acts_as_taggable_on with a collection
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!
精彩评论