2015-12-09 38 views
0

有沒有辦法預先考慮到活動的記錄查詢的現有order部分?如何預先考慮到訂購與範圍條款

我在我的Location模型中定義的以下關聯:

class Location < ActiveRecord::Base  
    has_many :location_parking_locations, -> { by_votes } 
    has_many :parking_locations, through: :location_parking_locations 
end 

LocationParkingLocation模型中,by_votes範圍定義:

class LocationParkingLocation < ActiveRecord::Base 
    belongs_to :location 
    belongs_to :parking_location 
    scope :by_votes, -> { order("upvotes - downvotes ASC, upvotes + downvotes DESC, id ASC") } 
end 

我想一個範圍添加到ParkingLocation模型它爲查詢添加了一個額外的範圍,但是我希望該範圍被添加到查詢的現有order部分。範圍是這樣的:

class ParkingLocation < ActiveRecord::Base 
    has_many :location_parking_locations 
    has_many :locations, through: :location_parking_locations 
    scope :with_pending, -> { order(creation_pending: :desc) } 
end 

我希望打電話location.parking_locations.with_pending,並取回parking_locations,通過投票有序的集合,但在收集開始的任意pending停車位置。這可能嗎?

回答

0

由於範圍是λ,你可以使用參數,例如:

scope :your_scope, -> (sort_order = :desc) { order(name: sort_order) } 

設置好它,因爲你需要和一個(可能是可選的)參數調用它:

your_model.your_scope(:desc)