I'm looking to generate "or" conditions for a query in a Rails app, from an array of words i want to try to match
against values in a single column, in Arel. I'm trying to follow advice in a very similar SO question with a good answer.
But the OP in that question left out explaining the method value.to_condition
where he turns his values into conditions. Basically i don't understand how to turn each word string into a chainable Arel condition.
This kind of thing is easy to understand for me when chaining SQL query strings, but in Arel, I don't know how to chain the conditions without executing them — the conditions are straight Ruby.
In the below, patterns
is my array of words. Each condition should be phrased Table.arel_table[:text].matches(pattern)
and I'd like each of these conditions joined with an .or
conditions = patterns.inject { |conds, cond| cond.or(conds) }
When I try to do this though, I don't understand if or how the chaining is working.
conditions = patterns.inject do |conds, cond|
Table.arel_table[:text].matches(pattern).or(conds)
end
Whoops - it's super easy with Arel.
Table.where(Table.arel_table[:text].matches_any(patterns))
I'd still like to know more about chaining Arel conditions.