这主要是基于RailsCasts的截图,341。
我试图使用PostgreSQL全文搜索,它是有效的,但我需要确定它的范围。搜索应该遍历object.state=='saved'
的对象。
在screencasts中,搜索方法的定义类似于类方法,所以我不能使用“where”,因为“where”返回数组,而搜索方法是类方法,所以这里有错误。我试图这样改变:
def test_search(query)
if query.present?
self.where("title @@ :q or text_for_test @@ :q", q:query)
else
scoped
end
end
致:
@tests=Test.where(:state=>'saved')
@tests.test_search(params[:query])
但比Rails抛出未定义的方法错误。
我还能做些什么来确定我的搜索范围?或者我该如何修复这个尝试的错误?
最佳答案
使用textaculargem对postgres进行全文搜索。
您可以使用textacular提供的“basic_search”函数,类似于如何使用命名作用域。
Test.basic_search('Sonic').where(:status => 'saved')
Test.where(:status => 'saved').basic_search('Sonic')
关于ruby-on-rails - PostgreSQL全文搜索:我如何确定范围?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16212701/