I have a product
model with a has_and_belongs_to_many
association with tags
. I am trying to create a custom query to exclude certain products with a particular tag. With the code below I get the error PG::UndefinedColumn: ERROR: column products.product_id does not exist...
If I remove .where.not(products_tags: 'Mouldings')
from the @stones
query, products will list based on the @products
model.
product.rb
class Product < ApplicationRecord
include PgSearch
pg_search_scope :search_for,
against: %i(name body),
associated_against: { variations: :name, categories: :name, tags: :name },
using: {
tsearch: {
any_word: true
}
}
has_and_belongs_to_many :categories
has_and_belongs_to_many :tags
...
end
tag.rb
class Tag < ApplicationRecord
has_and_belongs_to_many :products
end
products_controller.rb
...
def index
if params[:query]
@products = Product.search_for(params[:query])
@stones = @products.where.not(products_tags: 'Mouldings')
else
...
end
end
...