Question

I have an app that begins with Categories. A category has many products, and products can have tags. This app wouldn't suit having a tag cloud so I'm looking to just have a list of tags at the top of the page that works in the same way.

This is for an agriculture store, so there are different categories that may have the same tags - for example there may be Dog (category) food (tag) and Cat (category) food (tag). How would I make sure that only products related to the category and tag are displayed?

Thank you.

Was it helpful?

Solution 2

After fiddling with this for far too long, I couldn't get the above code to work. I also really didn't want to write direct tags into my controllers. So instead, I built a scaffold underneath categories called Sub Categories, which belongs_to Category and has_many products.

OTHER TIPS

Product.tagged_with("").joins(:category).where(category:{name: ''})

Product.tagged_with(["<food tag1>", "food tag2"], :match_all => true).joins(:category).where(category:{name: '<category>'})

To controll how the tags are used in Product retrieval you have these options:

Product.tagged_with(["x", "y"], :match_all => true)
Product.tagged_with(["x", "y"], :any => true)
Product.tagged_with(["x", "y"], :exclude => true)

In your controller:

def index

  @products = Product.tagged_with(params[:tags], :match_all => true).joins(:category).where(category:{name: params[:category})

end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top