Search code examples
rubysequel

Ruby Sequel and array of patterns


I have an array of patterns for use with ILIKE, an existing dataset with other filters, and I want to add it to that.

patterns = ['abc%', 'bcd', '%cde', ...]

I use Postgres, so in SQL I would do something like AND field ILIKE ANY('abc%', ...), but how to achieve that (or something similar, like dynamic AND (field ILIKE 'abc' OR field ILIKE 'bcd')? What would be the ideal solution with Sequel?

Thank you!


Solution

  • Either of the following should work

    where(ary.map {|val| Sequel.ilike(:column_name, val)}.reduce(&:|))
    #or
    where(Sequel.lit("table_name.column_name ILIKE ANY(#{ary.map(&:inspect).join(',')})"))
    

    Personally I would prefer the former but if you control the contents of ary then either should perform without issue.