gpt4 book ai didi

ruby-on-rails - 在 pg_search 查询后列出可搜索值

转载 作者:太空宇宙 更新时间:2023-11-03 17:54:04 24 4
gpt4 key购买 nike

有没有办法从模型而不是索引表条目中获取 pg_search 结果?我的代码有效,但我必须遍历结果。

search = PgSearch.multisearch(params[:search])

@items = Array.new
search.find_each do |result|
@items.push(result.searchable)
end

最佳答案

Multisearch 将始终返回 PgSearch::Document 记录。

如果你想直接查询你的模型,你可以定义一个pg_search_scope,例如:

pg_search_scope :custom_search, :against => [:title] # Can use multiple fields

然后将其与 search = Model.custom_search(params[:search]) 一起使用,这将返回 Model 记录。

此外,编写上述代码的一种更简洁的方法是:

search = PgSearch.multisearch(params[:search])
@items = search.map(&:searchable)

关于ruby-on-rails - 在 pg_search 查询后列出可搜索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17884791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com