Search code examples
jqueryruby-on-railsformsjquery-tokeninput

rails with jquery token-input: first character ignored in search


UsersController's index:

@users = current_user.other_users.where("first_name like ?", "%#{params[:q]}%")

1) Search works fine in the text field except that the first character isn't detected.

Example: If I search with "J" for "John" I won't get any results, but if I do "o" I will get "John" as I should.

2) I'm wondering how you add an additional search condition above for last_name...? I thought something like

.where("first_name like ? or last_name like ?", "%#{params[:q]}%")

would work, but it doesn't...

Help appreciated!


Solution

  • I'm betting you're using Postgres as your database, where LIKE clauses are case-sensitive. To search case-insensitive in Postgres:

    .where("first_name ILIKE ? or last_name ILIKE ?", "%#{params[:q]}%")
    

    This SO has plenty of background: How do you write a case insensitive query for both MySQL and Postgres?

    Other alternatives: