Search code examples
ruby-on-rails-3.2rails-activerecordarel

Cannot programmatically combine AND and OR conditions using Arel


Given the SQL conditions cond1, cond2 and cond3 generated using Arel operators (.eq for example), I cannot seem to use Arel to produce the SQL:

SELECT * FROM <table> WHERE (cond1 AND cond2) OR cond3

This is because to AND conditions together you use .where(), but you can't then .or() the result of .where(). You can only .or() conditions together inside a .where(). I.e. .where and .or are not on the same "level", I would guess one needs a dedicated .and() method on the same level as .or().

Note that the brackets around (cond1 AND cond2) denotes that these conditions are ANDed together at the same time, where the OR cond3 is added in another iteration of the loop. They make no difference to the result of the query.

I've read the Arel README and there is no example for this situation and I can't figure out how to dynamically build up this query (in a loop) using Squeel, which has an OR operator |.


Solution

  • Arel::Nodes::Grouping wraps nodes in parentheses.

    Arel::Nodes::Grouping.new(Arel::Nodes::And.new("blah", "there")).to_sql
    # => ('blah' AND 'there')