我有一个Customer
模型,每个客户都有很多工作:
class Customer < ActiveRecord::Base
has_many :jobs
end
class Job < ActiveRecord::Base
belongs_to :customer
def self.unbilled
finished.uninvoiced # these are other scopes on Job
end
end
如何在Customer
上定义一个范围,该范围将返回所有拥有未开单作业的客户的列表?
答案 0 :(得分:4)
您可以merge范围:
class Customer < ActiveRecord::Base
has_many :jobs
scope :freeloaders, joins(:jobs).merge(Job.unbilled)
end