2012-07-26 90 views
0

可以說我有3個模型叫狗,貓和老鼠。我希望所有三個能夠有相同的,不同的和許多類別。我也想添加邏輯到一個類別,並像其他模型一樣定期創建它。所以它可能是這樣的:允許不同模型具有相同和多個類別的設計?

Dog.categories = brown, spots, heavy fur, weak limbs 
Cat.categories = brown, red, heavy fur 
Mouse.categories = brown, small 

Category 
    has_and_belongs_to_many :dogs 
    has_and_belongs_to_many :cats 
    has_and_belongs_to_many :mouses 
    def watch_health 
    if self.name == "weak limbs" 
    do stuff here 
    end 
end 

現在說的。模型和桌子設計如何。我並不完全理解多態關聯在這種情況下是如何工作的,所以對於所有3來說看起來像HATBM將是正確的方式。你怎麼看?它是否正確?

+1

可以貓,狗鼠有很多種類嗎? – 2012-07-26 02:41:17

回答

0

好了,所以最後我做一個has_many :through協會,你可以從herehere得到更多的幫助。

相關問題