假设我有一些想要搜索的产品。
我想找的产品是在一些最小和/或最大值,如长度,宽度和高度。
我可以这样做:
any_of do
with(:length).greater_than_or_equal_to(length_min)
with(:length).less_than_or_equal_to(length_max)
with(:width).greater_than_or_equal_to(width_min)
with(:width).less_than_or_equal_to(width_min)
with(:height).greater_than_or_equal_to(height_min)
with(:height).less_than_or_equal_to(height_min)
end这将给我的产品,以匹配其中的任何一个。
我希望返回所有产品,但按符合最多标准的产品排序。
举个例子:
在所有长度、宽度和高度范围内的产品A首先是,然后是仅匹配长度和宽度的产品C,然后是不匹配任何长度、宽度和高度范围的产品B。
有人知道怎么做吗?
谢谢你,瑞克
发布于 2014-02-12 02:32:19
查看https://github.com/sunspot/sunspot上的文档,似乎通过使用any_of do,只要有任何项匹配任何条件,就会返回结果。
# Posts that do not have an expired time or have not yet expired
Post.search do
any_of do
with(:expired_at).greater_than(Time.now)
with(:expired_at, nil)
end
endhttps://stackoverflow.com/questions/21709406
复制相似问题