0

我正在尝试运行查询以查找给定位置的所有项目。我有一个Item.rb&Location.rb模型在哪里searchkick。在我的items控制器中,我有以下逻辑:

if params[:item_name].present?
    @items = Item.search(params[:query], index_name: [Item.searchkick_index.item_name, Location.searchkick_index.city])

:item_name用于捕获参数)

Ant以下是日志:

Parameters: {"utf8"=>"✓", "item_name"=>"test", "city"=>"Memphis", "commit"=>"Search"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 12]]
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 12]]
Completed 500 Internal Server Error in 3ms

NoMethodError (undefined method `item_name' for #<Searchkick::Index:0x007fb43858d4e8>):
app/controllers/items_controller.rb:9:in `index'

Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_source.erb (12.4ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_trace.html.erb (5.6ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_request_and_response.html.erb (1.4ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_web_console.html.erb (1.3ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/diagnostics.html.erb within rescues/layout (51.0ms)

我尝试重新索引位置和项目,重新启动服务器,但无济于事。

一个如何构建搜索两个/n 个模型?

4

1 回答 1

1

我相信您只需要指定索引的名称,例如

if params[:item_name].present?
    @items = Item.search(params[:query], index_name: [Item.searchkick_index.name, Location.searchkick_index.name])

Searchkick 将在您在 SearchKick 配置中为这些模型定义的任何字段中搜索 Item 和 Location 模型。

于 2015-07-23T21:17:51.030 回答