Ruby rails 中排序问题

yuzizhang · 2024年07月29日 · 最后由 conditionmechanic 回复于 2024年07月31日 · 342 次阅读

第一个问题 ScenicArea.order("hot_spot desc").order("id asc") 生成的查询是 SELECT scenic_areas.* FROM scenic_areas ORDER BY hot_spot desc, id asc ScenicArea.order("hot_spot desc, id asc") 生成的查询是。SELECT scenic_areas.* FROM scenic_areas ORDER BY hot_spot desc, id asc 为什么是相同的 连续调用 order 后面的不应该覆盖前面的吗

第二个问题 post"/scenic_area/list" do check_params_sign @scenic_areas = ScenicArea.publish if params[:city_id].present? @scenic_areas = ScenicArea.by_city_id(params[:city_id]) @distance = 5000 else @distance = 50 end if params[:order].present? && params[:order].to_s == 'hot' @scenic_areas = @scenic_areas.order("hot_spot desc") else @scenic_areas = @scenic_areas.order(:distance) end if (params[:latitude].present? and params[:longitude].present?) @scenic_areas = @scenic_areas.near([params[:latitude],params[:longitude]],@distance.to_i, units: :km) end @scenic_areas = @scenic_areas.paginate(:per_page=>per_page,:page=>page) present({"code": 0,"msg": "ok", "data": {"scenic_areas": @scenic_areas.collect{|c|c.as_json(false)},"current_page": page,"total_pages": @scenic_areas.total_pages}}) end 这样写返回的结果是我想要的按照热度排序的结果 post"/scenic_area/list" do check_params_sign @scenic_areas = ScenicArea.publish if params[:city_id].present? @scenic_areas = ScenicArea.by_city_id(params[:city_id]) @distance = 5000 else @distance = 50 end if (params[:latitude].present? and params[:longitude].present?) @scenic_areas = @scenic_areas.near([params[:latitude],params[:longitude]],@distance.to_i, units: :km) end if params[:order].present? && params[:order].to_s == 'hot' @scenic_areas = @scenic_areas.order("hot_spot desc") else @scenic_areas = @scenic_areas.order(:distance) end @scenic_areas = @scenic_areas.paginate(:per_page=>per_page,:page=>page) present({"code": 0,"msg": "ok", "data": {"scenic_areas": @scenic_areas.collect{|c|c.as_json(false)},"current_page": page,"total_pages": @scenic_areas.total_pages}}) end 这样把 order 排序放在后面反而不会按照热度排序 返回的是按照距离排序的结果 这两个问题都不是很清楚 希望有懂的帮我解答一下

排版好乱

需要覆盖用 reorder

jiting 回复

没毛病

需要 登录 后方可回复, 如果你还没有账号请 注册新账号