robzolkos
robzolkos

Reputation: 2266

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"]

Upvotes: 2

Views: 264

Answers (1)

Syed Aslam
Syed Aslam

Reputation: 8807

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!

Upvotes: 3

Related Questions