如何使用ps_search gem for Rails 5仅搜索批准的帖子

时间:2018-06-28 13:19:32

标签: ruby-on-rails ruby ruby-on-rails-5 pg-search

我正在对邮政和网站表格进行多次搜索。我有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]

1 个答案:

答案 0 :(得分:0)

尝试在所需模型中使用default_scope。例如,

class Post < ApplicationRecord
  include PgSearch
  default_scope { where(is_approved: true)}
  multisearchable :against => :title