用pg_search搜索多个模型 [英] Searching multiple models with pg_search

查看:108
本文介绍了用pg_search搜索多个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Rails应用程序中实现全文搜索。我使用PostgreSQL作为数据库,因此使用PostgreSQL的全文搜索功能。我有两个模型用户和记分板,我想针对特定列搜索这两个模型以提取记录。

I am trying to implement a full text search in my rails app. I am using PostgreSQL as a database, therefore, using PostgreSQL's full text search capabilities. I have two models Users and Scoreboards and I want to search through both models against specific columns to pull out records.

我正在使用pg_search gem进行全文搜索,我读了多元搜索文档并成功实施了所有操作。我已经创建了pg_search_documents表。代码如下。

I am using pg_search gem for full text search, I read the multisearch documentation and implemented everything successfully. I have created the pg_search_documents table. The code is given below.

记分板模型

include PgSearch
   multisearchable :against => [:name_of_organization, :name_of_activity, :name_of_scoreboard]

用户模型

include PgSearch
  multisearchable :against => :name

控制器代码

 def index
   @pg_search_documents = PgSearch.multisearch(params[:search])
 end

当我在以下位置运行代码<%= pg_search.searchable%> 时,搜索有效视图。当用户和记分板的每个对象都被搜索时,这将同时返回。

The search works when I run the code <%= pg_search.searchable %> in the view. This returns both the user and scoreboard OBJECTS when each one of them are searched.

查看代码

<% @pg_search_documents.each do |pg_search| %>
        <%= pg_search.searchable.name %>
        <%= pg_search.searchable.name_of_organization %>
<% end %>

但是,当我尝试运行上面给出的视图代码并搜索名称时,我会得到

However, when I try to run the view code given above and search for a name, I will get the following error,

undefined method `name_of_scoreboard' for #<User:0x007fa44ad18cd8>. 

当我尝试搜索name_of_scoreboard时,也会遇到类似的错误

I get a similar error when I try to search for the name_of_scoreboard,

`undefined method `name' for #<Scoreboard:0x00000006fa3560>.` 

如果我在视图中使用以下代码,则<%= @ pg_search_documents.searchable.id%> 显示用户和计分板的ID。这意味着我只能搜索两个模型中共同的列。

If I use the following code in my view, <%= @pg_search_documents.searchable.id %>, the id's for both the user and scoreboard show up. This means that I can only search against columns that are common in the two models.

如果我尝试搜索两个不同的列,例如 name和name_of_scoreboard ,则会遇到上面提到的两个错误。我可以在两个不同的模型之间进行搜索,但是无法从视图中显示结果。我不确定自己做错了什么,还是错过了重要的事情。我真的不确定。我试图使其简短,并试图包括相关信息。如果需要任何特定代码,请告诉我。与往常一样,我们将不胜感激任何帮助!

If I try to search for two different columns such as name and name_of_scoreboard, I get the two errors I mentioned above. I am able to search between two different models but I am not able to display the results from the view. I am not sure if I am doing something wrong or maybe I am missing something important. I am really not sure. I have tried to keep it brief and tried to include relevant information. If any specific code is required, please let me know. As always, Any help is greatly appreciated!!

推荐答案

我认为您正在混淆搜索并显示一列从-的结果看来,您的代码实际上是在多个模型的不同字段中搜索。然后,该搜索返回指向匹配对象的文档数组。您遇到的问题是您试图显示没有名称的对象的名称。我认为您想要的是以下内容。

I think you're mixing up "searching for" and displaying a column from the results of - It looks like your code is actually searching across different fields in multiple models. That search then returns an array of "documents" which point to the matching objects. The issue you're having is that you're trying to display the name of an object that doesn't have a name. I think something like the following is what you're going for.

<% @pg_search_documents.each do |pg_search| %>
  <% if pg_search.searchable.respond_to?(:name) %>
    <%= pg_search.searchable.name %>
  <% else %>
    <%= pg_search.searchable.name_of_organization %>
  <% end %>
<% end %>

如果结果对象具有名称(即-如果它是用户),它将显示名称。否则为组织名称(如果是记分板)。请注意,这不是一个很好的解决方案,因为它现在已经在您的视图中引入了逻辑,但是可以解释问题。

That will display the name if the result object has a name (ie - if its a user) and otherwise the organization name (if its a scoreboard). Note, this isn't a great solution since it's now introducing logic into your view but it explains the issue.

这篇关于用pg_search搜索多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆