Cameron Cameron - 2 months ago 12
Ruby Question

Ruby select from array where key is like parameter

I have a list of objects that I want to filter by a parameter in the URL.

So for example:

@items = items.select { |item| params[:q] == item.name }


However this will match EXACT... how can I make it behave as a LIKE?

Answer
@items = items.select { |item| item.name.include?(params[:q]) }

Edit:

If you need the matching to be case insensitive.

@items = items.select { |item| item.name.downcase.include?(params[:q].downcase) }