I’m attempting to write a SQL search which will allow return any records which are tagged with certain values, and exclude any of those results which have other tags.
Tags are applied using a join model, like this:
class Customer < ActiveRecord::Base
has_many :tag_assignments
has_many :tags, :through => :tag_assignments
end
class Tag < ActiveRecord::Base
has_many :tag_assignments
has_many :customers, :through => :tag_assignments
end
class TagAssignment < ActiveRecord::Base
belongs_to :customer
belongs_to :tag
end
The query I currently have is:
SELECT DISTINCT customers.* FROM customers LEFT OUTER JOIN tag_assignments ON tag_assignments.customer_id = customers.id WHERE (tag_assignments.tag_id NOT IN (?))
The ? is then replaced in a query by the list of tags I don’t want included.
This works fine when a customer has only a single tag applied, but as soon as they get multiple tags they’ll show up despite the exclusion, since one of their other tags does match.
Something to keep in mind is that this needs to continue working when additional clauses are added (such as requiring other tags to be present, or matching on other customer attributes), but any point in the right direction would be appreciated.
I’m rusty with this … but you need to get all things with tag’s first and then negate ..