2014-06-05 18 views
7

Ho due modelli per Show e Performance (come in una rappresentazione teatrale o commedia). Sono associati in questo modo nei modelli:ambito rotaie tramite has_many association

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

class Performance < ActiveRecord::Base 
    belongs_to :show 
end 

Nel modello Prestazioni è presente un datetime denominato: start_time.

Come definire un ambito nel modello che restituisce tutti gli spettacoli con almeno una prestazione di cui: start_time è in futuro?

Inoltre, come definire un ambito che restituisce tutti gli spettacoli che non presentano prestazioni in cui: start_time è in futuro?

+0

qual è la tua domanda? – kikicarbonell

+0

Pensavo fosse chiaro. Lo riformulerò come domande. Ci sono due. –

+0

ok ... @ KevinM se il mio suggerimento era giusto, per favore confermalo come soluzione – kikicarbonell

risposta

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