2012-02-05 5 views
0

Sunspot 검색에서 '또는'를 어떻게 사용할 수 있습니까? 내 코드에서또는/|| in Sunspot 검색

나는

with(:location_id, current_user.location.path_ids || current_user.location.parent.descendant_ids) 

검색은 첫 번째 부분을 평가 있습니다.

'current_user.location.path_ids'를 먼저 입력하면 (|| 이전), 그 검색 결과로 얻은 레코드 만 가져옵니다. 'current_user.location.parent.descendant_ids'를 먼저 입력하면 검색 결과가 표시됩니다. 나는 둘 다 결과를 원해.

나는 또한 당신이 내 질문을 이해 바랍니다

with(:location_id, current_user.location.path_ids || :location_id, current_user.location.parent.descendant_ids) 

을 시도했습니다.

답변

1

는, 내가 문제를 볼 수 있다고 생각합니다.

연산자가 논리 OR이므로 true로 평가되므로 왼쪽 피연산자가 반환됩니다.

나는 current_user.location.path_idscurrent_user.location.parent.descendant_ids이 둘 다 배열이라고 가정하므로 대신 | 연산자와 결합해야합니다.

with(:location_id, current_user.location.path_ids | current_user.location.parent.descendant_ids) 
+0

감사합니다! 나는 또한 두 배열을 + 대신에 +로 병합 할 수 있다는 것을 알았다. –

2

||은 루비 연산자입니다. 이 코드는 기본적으로 표현식 current_user.location.path_ids || current_user.location.parent.descendant_ids을 평가하고이 값을 with 메소드의 두 번째 인수로 전달합니다.

Sunspot README을 확인하십시오. 난 당신이 any_of 필요하다고 생각 : 나는 태양 흑점을 사용하지 않은 있지만

any_of do 
    with(:location_id, current_user.location.path_ids) 
    with(:location_id, current_user.location.parent.descendant_ids) 
end 
+0

감사합니다. 나는 그 것을 기억하려고 노력할 것이다. –

+0

다음 문제를 해결하기 위해이 방법을 사용했습니다. 고마워요 !! :-) –

관련 문제