Cbas Cbas - 11 months ago 27
Ruby Question

ILIKE search in string only works on its first part?

I have a

table with a
field which contains the users full name.

I wrote a search function that should allow finding users by their first name or their last name, but it only returns results for first names.

For example if I have a user: = 'John Smith'
, if I enter 'John' I get the record back, but if I enter 'Smith' I get no results. Here's my search code:

name_users = User.where('name ILIKE ?', params["search_text"] + '%').all

How can I allow searching by last name?

dkp dkp

Try this:

name_users = User.where('name ILIKE ?', "%#{params["search_text"]}%")