Rails 使用 Searchkick:通过连接获取结果 table
Rails with Searchkick: getting results with a joined table
我开始在我的应用程序中使用 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
现在,当我搜索 table houses
中的房屋数据时,searchkick 正在工作,但是如果我在地址 table 中查找字段,我根本没有得到任何结果。
可以这样做吗?
提前致谢!
我认为您还需要索引地址。为此,请将以下代码添加到您的 House
模型中:
def search_data
attributes.merge(
address_city: self.address.try(:city),
address_zip: self.address.try(:zip)
)
end
请根据您在 Address
模型中的字段随意修改上述代码。
另外,添加上面的代码后,别忘了重新索引。
我开始在我的应用程序中使用 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
现在,当我搜索 table houses
中的房屋数据时,searchkick 正在工作,但是如果我在地址 table 中查找字段,我根本没有得到任何结果。
可以这样做吗?
提前致谢!
我认为您还需要索引地址。为此,请将以下代码添加到您的 House
模型中:
def search_data
attributes.merge(
address_city: self.address.try(:city),
address_zip: self.address.try(:zip)
)
end
请根据您在 Address
模型中的字段随意修改上述代码。
另外,添加上面的代码后,别忘了重新索引。