代码之家  ›  专栏  ›  技术社区  ›  David Tuite

具有满足特定条件的相关记录的记录范围?

  •  1
  • David Tuite  · 技术社区  · 14 年前

    我有一个 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
    

    如何在上定义范围 顾客 哪个将返回所有拥有未绑定作业的客户的列表?

    1 回复  |  直到 14 年前
        1
  •  4
  •   rossta    14 年前

    你也许可以 merge 作用域:

    class Customer < ActiveRecord::Base
      has_many :jobs
    
      scope :freeloaders, joins(:jobs).merge(Job.unbilled)
    end