I have 2 models Product and Tag with many-to-many relationship.
class Product < ActiveRecord::Base
has_many :product_tags
has_many :tags, through: :product_tags
end
class Tag < ActiveRecord::Base
has_many :product_tags
has_many :products, through: :product_tags
end
and the relationship model:
class ProductTag < ActiveRecord::Base
belongs_to :product
belongs_to :tag
end
What would be the most optimal way to search for the products by the list of given tags? The products must have all the tags, not just one of them.
I found the answer here https://stackoverflow.com/a/11887362/808175. So in my case it is: