2014-06-05 2 views
7

나는 쇼와 퍼포먼스를위한 두 가지 모델을 가지고있다. 이들은 모델에서 이와 같이 연결됩니다.레일 스코프 has_many 연관

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

class Performance < ActiveRecord::Base 
    belongs_to :show 
end 

성능 모델에는 start_time이라는 datetime이 있습니다.

start_time이 미래의 성능이 하나 이상인 모든 프로그램을 반환하는 모델의 범위를 정의하려면 어떻게해야합니까?

또한 start_time이 미래에 어떤 공연이없는 모든 프로그램을 반환하는 범위를 어떻게 정의합니까?

+0

무엇이 질문입니까? – kikicarbonell

+0

나는 그것이 명백하다고 생각했다. 나는 그것을 질문으로 바꿀 것이다. 두 가지가있다. –

+0

ok ... @ KevinM 제 제안이 맞다면 솔루션 – kikicarbonell

답변

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