I have a problem with search on PG. I have a table with two columns first name and last name. I want someone to be able to search for one person using their first name, their last name, or the full name. I was following the Railscast #343 on full text search for PG and for what I understand, the search is supposed to work as I intend it to work but it doesn't. The code below will only return results when you put the exact first name or last name; it will not return anything is you put the first name and last name or if you only put a part of the first name or last name.
I would appreciate any input on how to make this work as I intend.
Model
def self.text_search(query)
if query.present?
where("first_name @@ :q or last_name @@ :q", q: query)
else
where(nil)
end
end
Controller
def index
@members = @organization.members.text_search(params[:query]).page(params[:page]).per(20)
end
Console Output
SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "members" WHERE "members"."organization_id" = $1 AND (first_name @@ 'jon' or last_name @@ 'jon') AND (expiration_date <= '2014-11-30') AND (expiration_date > '2014-10-31') LIMIT 20 OFFSET 0)