L
L
lexxeice2019-07-07 21:32:20
PostgreSQL
lexxeice, 2019-07-07 21:32:20

Rails 5, how to fetch database from multiple fields?

The bottom line is this, I make a user search panel on the page, and process the entered values ​​as follows:
users = User.where("first_name LIKE ? or last_name LIKE ?", "%#{params[:search]}%",
"%#{ params[:search]}%") if params[:search].present?
If you search only by first name (first_name) or last name (last_name), then everything works fine, but if you enter the first and last name at once, for example, "Ivan Ivanov", then the search does not work accordingly.
I'm not good at querying the database, can you tell me how to process another option with the entered first name and last name at the same time?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lexxeice, 2019-07-07
@lexxeice

I found it myself, everything turned out to be quite simple, the code will take the following form:
users = User.where("first_name || ' ' || last_name like ?", "%#{params[:search]}%") if params[: search].present?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question