简体   繁体   English

如何通过远距离以外的其他方式订购rails geocoder gem的结果

[英]How do I order the results of rails geocoder gem by something other than distance

I have this excellent gem https://github.com/alexreisner/geocoder working fine in my app but I need to order the results of an active record query slightly differently. 我有这个优秀的宝石https://github.com/alexreisner/geocoder在我的应用程序中正常工作,但我需要略微不同地订购活动记录查询的结果。

For example using the rails console 例如,使用rails控制台

$> Person.near('London',10)

produces a long SQL query (not relevant) followed by 生成一个长SQL查询(不相关)后跟

ORDER BY distance ASC

This gives a nice ordered list of People from closest to furthest (upto 10 miles away). 这给出了一个很好的有序人员列表,从最近到最远(最远10英里)。 Now if I do this 现在,如果我这样做

$> Person.near('London',10).order('price_per_hour')

The query is appended by 查询附加

ORDER BY distance ASC, price_per_hour

I still want all the people near London but I dont want it ordering by distance. 我仍然想要伦敦附近的所有人,但我不希望它按距离排序。 There doesnt seem to be anyway of taking this out. 似乎无论如何都没有把它拿出来。

I can't sort outside of the active record query because I am using pagination so ordering needs to be done in the main query which is then paginated. 我无法在活动记录查询之外进行排序,因为我正在使用分页,因此需要在主查询中进行排序,然后对其进行分页。

You should try reorder instead of order : 你应该尝试reorder而不是order

Person.near('London',10).reorder('price_per_hour')

The reorder removes and replace the order statement created by the scope near . reorder将删除并替换由范围near创建的order语句。

I know its too late. 我知道为时已晚。 Now geocoder gem provides to remove default order of distance 现在geocoder gem提供了删除默认的距离顺序

Person.near('London',10,order: false).order('price_per_hour')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM