pg_search 进行高级搜索 [英] pg_search for an advanced search

查看:39
本文介绍了pg_search 进行高级搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何使用 pg_search_gem 和 pg_search_scope 将多个参数传递给我的搜索

I'm trying to figure out how to pass multiple parameters to my search using pg_search_gem and the pg_search_scope

这是我的简单搜索

include PgSearch
  pg_search_scope :simple_search, against: [:title, :description], using: { tsearch: { dictionary: "spanish"} } 

def self.search(search)
    if search.present?
        simple_search(search)
    else
        find(:all)
    end
end

但现在我正在尝试做这样的事情

But now I'm trying to do something like this

include PgSearch
  pg_search_scope :simple_search, against: [:title, :place, :category], using: { tsearch: { dictionary: "spanish"} } 

def self.searchadv(title, place, category)
  simple_search(:title => title, :place => place, :category => category)
end

我知道这是完全错误的,但这是我的询问.

I know this is totally wrong but this is my inquiry.

更新

这是新代码

  pg_search_scope :advance_search, lambda {|*args, query|
      return {:against => args,
      :query => query,
      using: { tsearch: { dictionary: "spanish"} } }
  }

def self.searchadv(query, title, place, category)
    advance_search(:all, :title => title, :place => place, :category => category)
end

还是不行.但是快完成了!我收到此错误:

Still not working. But it's almost done! I'm getting this error:

wrong number of arguments (3 for 4)

推荐答案

4 年前,我正在寻找这个问题的答案.所以,这就是我发现的并且它有效.也许它会帮助某人:

4 years ago and I'm looking for the answer of this question. So, here is what I found and it works. Maybe it'll help someone:

我将 2 个数组传递给搜索范围,一个包含列,另一个包含查询.因此,您可以根据需要添加任意数量的列和查询.

I'm passing 2 arrays to the search scope, one with the columns and other with the queries. So you'll be able to add as many columns and queries as you want.

include PgSearch
  pg_search_scope :advanced_search, (lambda do |args, query|
    return {
      :against => args, :query => query
    }
  end)

Model.advanced_search([:name, :current_status, :user_id],
            [params[:q_name], params[:q_status], params[:q_user]])

这篇关于pg_search 进行高级搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆