Grouping ands and ors in AREL Grouping ands and ors in AREL sql sql

Grouping ands and ors in AREL

You can generate parentheses using Arel::Nodes::Grouping.

participants ="participants")arel = participants.grouping(  participants[:accepted].eq(false).and(participants[:contact_id].eq(1))).or(participants[:id].eq(nil))arel.to_sql # => (("participants"."accepted" = 'f' AND "participants"."contact_id" = 1) OR "participants"."id" IS NULL)

I believe that according to the operator precedence

The problems is that AND has higher precedence than OR. So 1 AND 2 OR 3 is equivalent to (1 AND 2) OR 3. As a side note: if you use a wrapper like this one, you can write better looking code with Arel:

User.where((User[:created_at] > 3.days.ago) & (User[:enabled] == true))

Why not flip them around. Should be equivalent to:


hopefully I've got the parens properly set in the above, but you see what I mean...