我有以下型號設置:如何通過多態類型獲取對象的所有實例?
class Sound < ActiveRecord::Base
has_many :tenders, :as => :tenderable
belongs_to :event
end
class Stage < ActiveRecord::Base
has_many :tenders, :as => :tenderable
belongs_to :event
end
class Tender < ActiveRecord::Base
attr_accessible :event_id, :user_id, :estimate, :tenderable_id, :tenderable_type
belongs_to :tenderable, :polymorphic => :true
end
class Event < ActiveRecord::Base
attr_accessible :name
has_one :stage
has_one :sound
accepts_nested_attributes_for :stage, :allow_destroy => true
accepts_nested_attributes_for :sound, :allow_destroy => true
end
每個事件的各種Tenderable的連接到它(如舞臺,音響),我可以通過Event.find(ID).tenderables訪問這些,但我需要確定哪些「機會」可用,而不管他們所附的事件如何。
目前,我沒有對「機會」的模型,因爲我試圖讓事情變得簡單。
真的是我想要做的是一樣的東西Tenderables.all將返回所有的「聲音」,「階段」和其他任何我定義爲「tenderable」。
達到此目的的最佳方法是什麼?
感謝;)
你想找到所有'Tenderables'有一個'Tender'或者那些可能得到一個'Tender'所有對象(因爲這將只是所有'階段,聲音,...')。 – nathanvda
嗨Nathanvda - 我需要所有可能會招標的物件。 'tenderables'階段,聲音......列表可能會經常發生變化...... – significance