具有符合某些条件的相关记录的记录的范围?

时间:2012-09-28 02:11:42

标签: ruby-on-rails ruby ruby-on-rails-3 activerecord ruby-on-rails-3.2

我有一个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上定义一个范围,该范围将返回所有拥有未开单作业的客户的列表?

1 个答案:

答案 0 :(得分:4)

您可以merge范围:

class Customer < ActiveRecord::Base
  has_many :jobs

  scope :freeloaders, joins(:jobs).merge(Job.unbilled)
end