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)

我希望你能理解我的问题。

4

2 回答 2

2

||是一个 Ruby 运算符。这段代码基本上是评估表达式current_user.location.path_ids || current_user.location.parent.descendant_ids并将该值作为第二个参数传递给with方法。

查看太阳黑子自述文件。我认为你需要any_of

any_of do
  with(:location_id, current_user.location.path_ids)
  with(:location_id, current_user.location.parent.descendant_ids)
end
于 2012-02-05T14:13:59.670 回答
1

虽然我没有使用过 Sunspot,但我想我可以看到问题所在。

运算符是逻辑或,||因此它返回左操作数,因为它的计算结果为真。

我假设current_user.location.path_idsandcurrent_user.location.parent.descendant_ids都是数组,因此您需要将它们与|运算符结合起来。

with(:location_id, current_user.location.path_ids | current_user.location.parent.descendant_ids)
于 2012-02-05T14:21:28.710 回答