2012-07-26 71 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得到更多的帮助。

相关问题