Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to put each element from array as a parameter?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]

>Solution :

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading