1

我有一个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该范围将返回所有未开票工作的客户列表?

4

1 回答 1

4

您也许可以合并范围:

class Customer < ActiveRecord::Base
  has_many :jobs

  scope :freeloaders, joins(:jobs).merge(Job.unbilled)
end
于 2012-09-28T02:40:34.340 回答