我正在对邮政和网站表格进行多次搜索。我有is_approved布尔值的所有记录。当前,pg_search查找所有帖子,但是我只需要显示具有is_approved == true的帖子。
我知道我可以在is_approved字段中以“ if”状态签入循环,但听起来效率不高。我相信pg_search将有更好的限制方法。
干杯
_header.html.erb
<%= form_tag search_index_path, method: :get do %>
<%= text_field_tag :query, params[:query], placeholder: "Search" %>
<% end %>
search_controller.rb
class SearchController < ApplicationController
def index
@pg_search_result = PgSearch.multisearch(params[:query])
end
end
pg_search.rb
PgSearch.multisearch_options = {
using: {
tsearch: { dictionary: 'english' }
}
}
post.rb
class Post < ApplicationRecord
include PgSearch
multisearchable :against => :title
website.rb
class Website < ApplicationRecord
include PgSearch
multisearchable :against => [:title, :website_url]
答案 0 :(得分:0)
尝试在所需模型中使用default_scope。例如,
class Post < ApplicationRecord
include PgSearch
default_scope { where(is_approved: true)}
multisearchable :against => :title