I'm trying to write a concern to allow search on multiple fields from different models. I want to insert a search_fields array and generate a where OR query with the params[:query] value like this :
scope = scope.where("LOWER(user_name) LIKE LOWER(?)", "%#{params[:query]}%")
it works fine for one value but I need to receive an array of column names (different for different models) and generate a where or query like this (exemple with an array of two items) :
search_fields = ["title", "user_name"]
where("LOWER(title) LIKE LOWER(?) OR LOWER(user_name) LIKE LOWER(?)", "%#{params[:query]}%", "%#{params[:query]}%")
I generate the query like this :
query = search_fields.map do |s|
"LOWER(#{s}) LIKE LOWER(?)"
end.join(' OR ')
and it seems that there is no where_or method implemented. I could not do it by any means, is there any ideas how it is possible to do ?
Thanks for all answers