Mostly this is based on RailsCasts screencasts, #341.
I'm trying to use PostgreSQL full text search, and it works, but I need to scope it. Search should go through objects where object.state=='saved'
.
In screencasts method for search is defined like class method, so I can't use 'where' because 'where' returns Array, and search method is class method, so error here. I tried to change it this way:
def test_search(query)
if query.present?
self.where("title @@ :q or text_for_test @@ :q", q:query)
else
scoped
end
end
to:
@tests=Test.where(:state=>'saved')
@tests.test_search(params[:query])
But than Rails throws undefined method error.
What else can I do to scope my search? Or how do I fix error of this attempt?
Use textacular gem which gives full-text search for postgres.
You can use the "basic_search" function given by textacular similar to how you use named scopes.
Test.basic_search('Sonic').where(:status => 'saved')
Test.where(:status => 'saved').basic_search('Sonic')
where("title @@ :q or text_for_test @@ :q and state @@ :state", q:query, state:'saved')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.