pg_search for an advanced search

狂风中的少年 提交于 2021-02-11 05:51:27

问题


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

This is my simple search

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.


UPDATE

here is the new code

  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)

回答1:


You can use dynamic search scopes

include PgSearch
  pg_search_scope :simple_search, lambda do |query, *args| 
    return { :against => args, :query => query }
  end

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



回答2:


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:

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]])


来源:https://stackoverflow.com/questions/14223301/pg-search-for-an-advanced-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!