我有一个关于 pg_search 的问题:我在 solidus 2.9 上安装了 gem,并添加了一个 product_decorator.rb 模型,如下所示:
Spree::Product.class_eval do
include PgSearch::Model
pg_search_scope :search, against: [:name, :description, :meta_description, :meta_keywords],
using: {tsearch: {dictionary: "english"}}
end
它在 Rails 控制台中运行良好。
当我在solidus的前端使用搜索字段时,如何使它工作?我尝试将其添加到产品控制器,但似乎无法使其工作。谢谢!
更新
所以在 kennyadsl 的评论之后,我有这个:
#lib/mystore/products_search.rb
module MyStore
class ProductSearch < Spree::Core::Search::Base
def retrieve_products
Spree::Product.pg_search
end
end
end
#models/spree/product_decorator.rb
Spree::Product.class_eval do
include PgSearch::Model
pg_search_scope :search, against: [:name, :description, :meta_description, :meta_keywords], using: {tsearch: {dictionary: "english"}}
def self.text_search(keywords)
if keywords.present?
search(keywords)
else
Spree::Product.all
end
end
end
#controllers/products_controller.rb
def index
@searcher = build_searcher(params.merge(include_images: true))
@products = @searcher.retrieve_products(params)
end