If I want to create a category and be able to link products to it by tags I can like so:
- Create the category and product tables.
- Create a tags table with tags like: Ruby, Earrings, White-Gold
- Create a
category_tags
andproduct_tags
table to map them - Set category and product to hasAndBelongsToMany tags
- Set tags to hasAndBelongsToMany products and hasAndBelongsToMany categories
Now say I have 2 products one with tags: Ruby and Earrings and another with tags: Ruby and Bracelet
Say I want to create a Ruby Earrings category.
I could add the Ruby and Earrings tags to the category. But under normal HABTM Model associations both products will be returned because even though only 1 has an earrings
tag they both have a ruby
tag.
How can I make it only match products that have ALL of the same tags as the category (products can have more tags but must have all the tags the corresponding category has) in order be returned?
Also, taking that even further, how could I add -tags
to a category that the products must NOT have these tags to be returned?