0

I want to search a table for a list of pair values. Ex. This is an array of first and last names.

array = [['jane','doe'],['jack','chen'],['jane','ty'],['mike','ji'],['mike','smith']] 

I want to search the User table for each one of these combinations. Currently, I can only think of running a query per combination.

array.each do |a| User.where("firstname like (?) and lastname like (?)",a[0],a[1]) end 

Is there any way of running all the queries in a single query? Maintaining the combinations is required.

1 Answer 1

1

Following should work for you

query_string_array = [] query_array = [] array.each do |a| query_string_array << "(firstname like '%?%' AND lastname like '%?%')" query_array << a end User.where(query_string_array.join(" OR "), *query_array.flatten) 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Salil. This is very useful

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.