0

I have an array like this

matches = [1,2,3,4]

now i want to put that array in my query

.where(name LIKE ? AND last_name LIKE ? AND skin LIKE ?, matches[0], matches[1], matches[2]...)

sometime my query is

name LIKE ? AND last_name LIKE ? 

for that only have to be the match two

matches[0], matches[1]

or

name LIKE ? for this only have to be the match matches[0]

1 Answer 1

1

Just use splat-operator. It will convert array to arguments

matches = %w[a b c]


Person.where('name LIKE ? AND last_name LIKE ? AND skin LIKE ?', *matches)

Person.where('name LIKE ? AND last_name LIKE ?', *matches[0..1])

Or may be hash with double splat

matches = { last_name: 'a',  name: 'b',  skin: 'c' }

Person.where('name :name ? AND last_name LIKE :last_name AND skin LIKE :skin', **matches)

Person.where('name :name ? AND last_name LIKE :last_name', **matches.slice(:name, :last_name))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.