问题
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