问题
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