pg_search模型和控制器关系

pg_search model and controller relation

这可能是一个笨蛋。我正在构建一个搜索表单。

在模型 document.rb 中,我有这个:

pg_search_scope :search_full_text, 
                :against => :full_text, 
                :using => {
                    :tsearch => {
                        :prefix => true
                        }
                    }

在 documents_controller.rb 中,我有:

def find
    $results = Document.search_full_text(params[:ch_acte][:text])
end

但是没有任何东西被发送到数据库。服务器日志只说:

Started POST "/documents/find" for ::1 at 2017-01-19 08:48:07 +0100
Processing by DocumentsController#find as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ZkqVYMuqMqnUjLer/FVdBdtv4cycp71dXqPQw6j0mfHKX5ptin7p7YiYFj8bNtjciQDmHzbKtBnZoILpGGvl8Q==", "ch_acte"=>{"text"=>"complet", "words"=>"", #[cut for brievity]}, "commit"=>"Cherche"}
Rendering documents/find.html.erb within layouts/messources
Rendered documents/find.html.erb within layouts/messources (0.4ms)
Completed 200 OK in 216ms (Views: 210.2ms | ActiveRecord: 0.0ms)

除了模型中的方法 pg_search_scope 并在控制器中调用该方法,我还必须做什么才能将其发送到数据库?

当我在 rails 控制台中 运行 Document.search_full_text("esp") 时,它工作正常。

更新

我在 documents/find.html.erb 中添加了这个:

<% $results.each do |m| %>
    <p>The id is <%= m.id %>.</p>
<% end %>

我得到一个显示我的菜单的页面,然后只有白色...

您应该明白 Rails 试图尽可能地提高性能。当您构建搜索时,它不会执行,直到您尝试访问搜索结果。

所以你会做...

def find
  @documents = Document.search_full_text(params[:ch_acte][:text])
end

然后在你的 find.html.erb 你可能会做...

<% @documents.each do |document| %>
  <%= document.name %>
<% end %>

查询 在执行 @documents.each 行以检索文档时执行...即仅在需要执行时执行。