如何创建范围来订购城市名称? [英] How to create a scope to order cities name?

查看:57
本文介绍了如何创建范围来订购城市名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型/表:Services 和 City

I have two models/tables: Services and City

服务模式:

belongs_to :origin_city, class_name: 'City'
belongs_to :destiny_city, class_name: 'City'

如何创建范围以对城市名称进行排序?

How do I create a scope to order cities name?

我正在尝试这样的事情:

I'm trying something like that:

scope :by_city_name, -> { joins(:city).order("cities.name asc") }

但我只想在一个范围内订购服务来源城市.并在另一个范围内服务命运之城.

But I just want to order the service origin city in one scope. And service destiny city in another scope.

推荐答案

所以你应该只加入origin_city:

scope :by_origin_city_name, -> { joins(:origin_city).order('cities.name asc') }

这篇关于如何创建范围来订购城市名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆