【发布时间】:2018-03-18 22:31:20
【问题描述】:
我有一个product 模型与has_and_belongs_to_many 关联与tags。我正在尝试创建一个自定义查询以排除具有特定标签的某些产品。使用下面的代码,我得到错误PG::UndefinedColumn: ERROR: column products.product_id does not exist... 如果我从@stones 查询中删除.where.not(products_tags: 'Mouldings'),产品将根据@products 模型列出。
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
...
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-5