How to create a scope to order cities name?

六月ゝ 毕业季﹏ 提交于 2019-12-10 11:49:50

问题


I have two models/tables: Services and City

Service model:

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.


回答1:


So you should join only origin_city:

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


来源:https://stackoverflow.com/questions/27816930/how-to-create-a-scope-to-order-cities-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!