I have a search field where the user can enter "Param1" and my controller contains:
Model.where('column_name LIKE ?', "%#{search_field}%")
This is converted to:
SELECT ... FROM table WHERE column_name LIKE '%Param1%'
This works fine if the user enters "Param1" but I want make it comma separated search field.
So if the user enters "Param1,param2" or "Param1,Param2,Param3" the WHERE LIKE clause should work, My idea is to split the string based on ',' and have absolutely no idea on how to build the Model.where clause here, since it can be 1 parameter or 4.
In case you're using PostgreSQL, you can use ANY passing the mapped array of params:
Model.where('name LIKE ANY(array[?])', params.split(',').map { |val| "%#{val}%" })
# SELECT "models".*
# FROM "models"
# WHERE (name LIKE ANY(array['%param1%','%param2%']))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With