rails scope通过has_many关联

Kev*_*n K 7 scope associations has-many ruby-on-rails-4

我有两个模特用于表演和表演(在戏剧或喜剧节目中表演).它们在模型中如下关联:

class Show < ActiveRecord::Base
  has_many :performances, :dependent => :destroy
  accepts_nested_attributes_for :performances
end

class Performance < ActiveRecord::Base
  belongs_to :show
end
Run Code Online (Sandbox Code Playgroud)

在Performance模型中有一个名为start_time的日期时间.

如何在模型中定义范围,该范围返回至少具有以下性能的所有节目:start_time是将来的?

另外,如何定义一个范围,该范围返回所有没有任何性能的节目:start_time是将来的?

kik*_*ell 7

class Show < ActiveRecord::Base
   has_many :performances, :dependent => :destroy
   accepts_nested_attributes_for :performances

   scope :shows_with_pending_performance, includes(:performances).where("performances.start_time >= ? ", Date.today)
end

class Performance < ActiveRecord::Base
   belongs_to :show
end
Run Code Online (Sandbox Code Playgroud)