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]
CodePudding user response:
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))