2009-07-20 85 views
0

好吧,我有一些奇怪的情況(好像我的其他人都沒有...)。基本上我有那裏有4個實體的設置:(Rails)其他不相關的實體之間的各種關聯?

Sites -> Buildings -> Meters -> Values 

我再有第五實體(圖表)創建的價值觀報告。我有一個請求,允許圖表在視覺上與任何這些項目相關聯,以適應最終用戶。基本上每個圖表一次只能與任何一個實體關聯。是否有質量的「ACTS_AS」或瘋狂的東西,直觀地讓我可以將圖表與任何和所有實體關聯起來,而無需爲每個新關聯添加表格?

最好。

回答

0

也許你想要多態關聯。

class Chart < ActiveRecord::Base 
    # charts table has a chartable_id and a chartable_type column. Type is the 
    # class name of the associated chartable: Site, Building, etc. 
    belongs_to :chartable, :polymorphic => true 
end 

class Site < ActiveRecord::Base 
    has_one :chart, :as => :chartable 
end 

class Building < ActiveRecord::Base 
    has_one :chart, :as => :chartable 
end 

# ... 
+0

非常好。我在包裝界面的外觀上遇到了一些麻煩。 Drop box?複選框? *劃痕頭* – 2009-07-20 18:43:19