使用pg_search Gem进行Rails API搜索

问题描述

我有一个后端带有Rails API的应用程序-我正在尝试使用pg_search(首次使用gem)来实现搜索功能。我一直在使用几个不同的教程作为参考,但是API端点返回一个空数组。

当我访问/api/v1/search/nancy时,它会显示一个空数组[]-即使我有一个Nancy作为:name_first字段的字符。

我的大部分代码都在使用本教程,尽管我似乎并不掌握所有presenters部分,但这可能是一个问题。 https://devblast.com/r/master-ruby-web-apis/full-text-search

routes.rb

  scope 'api' do
    scope 'v1' do
      resources :characters
      resources :roles
      resources :armors

     
      get '/search/:text',to: 'search#index'
    end
  end

search_controller.rb

class SearchController < ApplicationController

  def index
    @text = params[:text]
    scope = PgSearch.multisearch(@text).includes(:searchable)
    render json: scope.to_json
  end

end

character.rb

class Character < ApplicationRecord
    include PgSearch
    multisearchable against: :name_first

    belongs_to :role
    belongs_to :armor
    ...
end

role.rb

class Role < ApplicationRecord
    include PgSearch
    multisearchable against: :name
  
    has_many :character
    accepts_nested_attributes_for :character,allow_destroy: true
end

armor.rb

class Armor < ApplicationRecord
    include PgSearch
    multisearchable against: :name

    has_many :character
    accepts_nested_attributes_for  :character,allow_destroy: true
end

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...