I have books and tags.
def Book < ApplicationRecord
has_and_belongs_to_many :tags
end
def Tag < ApplicationRecord
has_and_belongs_to_many :books
end
I want to find all books that do not have the tag with the id 1 associated. (They may have no tags.) I tried this:
Book.includes(:tags).where.not(tags: { id: 1 })
This query finds all books without tags, books that have other tags and books that have the unwanted tag and at least one other tag associated to them.
How can I filter all books with the certain tag? Thanks for any ideas!