2012-02-05 73 views
0

如何在太陽黑子搜索中使用'或'?使用或||在太陽黑子搜索

在我的代碼有

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

雖然我沒有用太陽黑子,我想我可以看到這個問題。

||運算符是邏輯或如此,因爲它計算爲真則返回左操作數。

我假設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

謝謝!我發現我也可以用+來代替||來合併兩個數組 – 2012-02-05 14:26:55

2

||是一個Ruby運算符。此代碼基本上是評估表達式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

感謝的!我會盡力記住那一個。 – 2012-02-05 14:27:04

+0

我用這個來解決我的下一個問題!非常感謝!! :-) – 2012-02-05 15:06:18