【问题标题】:Rails with Searchkick: getting results with a joined table带有 Searchkick 的 Rails:使用连接表获取结果
【发布时间】:2018-11-24 16:34:04
【问题描述】:

我开始在我的应用中使用 searchkick,我有两个模型:House 和 Address(属于房子)。

房子:

class House < ApplicationRecord
 searchkick
 has_one :address,  dependent: :destroy
end

地址:

class Address < ApplicationRecord
 searchkick
 belongs_to :house
end

在我的控制器中

def index
 if params[:term].present?
  @houses = House.search(params[:term])
 else
  @houses = House.search('*')
 end
end

现在,当我搜索表 houses 中的房屋数据时,searchkick 正在工作,但例如,如果我在地址表中查找一个字段,我根本没有得到任何结果。 可以这样做吗?

提前非常感谢!

【问题讨论】:

    标签: ruby-on-rails elasticsearch searchkick


    【解决方案1】:

    我认为您还需要索引地址。为此,请将以下代码添加到您的 House 模型中:

    def search_data
      attributes.merge(
          address_city: self.address.try(:city),
          address_zip: self.address.try(:zip)
      )
    end
    

    请根据您在Address 模型中的字段随意修改上述代码。

    另外,添加上述代码后,别忘了重新索引。

    【讨论】:

    • 它并没有真正起作用:undefined local variable or method addresses' for #<0x00007fb78071ddd0>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多