我在 product
和 variation
模型之间有很多关系 . 我也在使用pg_search来帮助过滤搜索结果 .
我需要显示过滤搜索结果中的产品总数以及这些过滤产品的变体总数 .
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_many :variations, -> { order(:order) }, dependent: :destroy
...
end
variation.rb
class Variation < ApplicationRecord
...
has_many :products
...
end
products_controller.rb 下面的@total_items我无法弄清楚如何制作(params [:query])的一部分
...
def index
if params[:query]
@products = Product.search_for(params[:query])
@total_collections = Product.search_for(params[:query]).count
@total_items = Variation.joins(:product).count
else
...
end
end
...