我正试图弄清楚如何使用pg_search_gem和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
但现在我正在尝试做这样的事情
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
我知道这是完全错误的,但这是我的询问。
更新
这是新代码
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
仍然无法正常工作。但它几乎完成了!我收到了这个错误:
wrong number of arguments (3 for 4)
答案 0 :(得分:1)
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
答案 1 :(得分:1)
4年前,我正在寻找这个问题的答案。所以,这是我发现的,它的工作原理。也许它会帮助某人:
我将2个数组传递给搜索范围,一个包含列,另一个包含查询。因此,您可以根据需要添加任意数量的列和查询。
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]])