2014-09-24 2 views
3

작업 우선 순위와 관련하여 ANDOR과 함께 여러 조건을 구성하는 것에 대한 질문이 있습니다. 내가 Arel으로 재 작성AND 및 OR이있는 복잡한 Arel 조건

where("NOT ((assignments.to IS NOT NULL AND assignments.to < :start_date) OR assignments.from > :end_date)", start_date: date.at_beginning_of_week, end_date: date.at_end_of_week) 

:

그래서, 나는 where 방법에 전달하는 SQL 문자열을 다음과 생성 할 필요가

table = Assignment.arel_table 
where(
    table[:from].gt(date.at_end_of_week). 
    or(
    table[:to].not_eq(nil).and(table[:to].lt(date.at_end_of_week)) 
).not 
) 

그러나 Arel는 조건 주위에 괄호를 넣어하지 않습니다 결과로 AND과이 조건은 잘못된 데이터를 선택합니다. 이 상태에서 어떻게 괄호를 쓸 수 있습니까?

답변

5

아니기 때문에 어떤 경우에 당신이 루비에서 SQL에서 NOT 또는 unless을 사용하지 않아야합니다 표현식을 괄호로 묶으면 모든 것이 이렇게 될 수 있습니다.

table = Assignment.arel_table 
where(
    table[:from].gt(date.at_end_of_week). 
    or(
    table.grouping(table[:to].not_eq(nil).and(table[:to].lt(date.at_end_of_week))) 
).not 
) 
-1

당신은 시도 할 수 :

table[:from].lt(end_date).and(
    table[:to].eq(nil).or(table[:to].gt(start_date)) 
) 

어쩌면 내가 뭔가를 그리워하지만, 당신은에 table.grouping 방법을 사용할 수 있습니다 것이 좋습니다

+0

'사용하지 않는 것이 가능하지만 그렇지 않을 수도 있습니다. 나의 경우에는. 그러나 어쨌든 내 질문은 그룹화가 아니라 '아닌'에 관한 질문이었습니다. – bronislav