module TokenizedLike extend ActiveSupport::Concern module ClassMethods def matches_tokens(attribute, tokens) table = self.arel_table query = self.all tokens.each do |t| match1 = "#{t}%" match2 = "% #{t}%" match3 = "%,#{t}%" matcher = ->(m) { table[attribute.to_sym].matches(m) } cond = matcher.call(match1).or(matcher.call(match2)).or(matcher.call(match3)) query = query.where(cond) end query end end end