I am attempting to do a search in Rails 3 like so. I have a database object like so with an attribute like so:
@food.fruit = "Apples, Pears, Plums"
and I have a search that uses params from a checkbox, like so:
params[:search] = ["Apples", "Oranges", "Bananas", "Grapefruit"]
Since my @food object above has "Apples" and the search array has "Apples" I'd like for this search to be successful. However I am having trouble having this work properly.
My initial thought was to do something like
@results = Food.where("fruit LIKE ?", "%params[:search]%")
or
@results = Food.where("fruit IN ?", params[:search])
But the first only works if the params[:search] contains ONLY the @food.fruit elements and no others. The second doesn't work at all.
My last ditch resort is to do something like
@results = Array.new
params[:search].each do |search|
@results << Food.where("fruit LIKE ?", search)
end
but I'd rather not do that if I don't have to. Anyone have any advice?