假设我有 3 个模型,分别称为 Dog、Cat 和 Mouse。我希望这三个都能够拥有相同、不同和许多类别。我还想向一个类别添加逻辑并像其他模型一样定期创建它。所以它可能是这样的:
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 都是正确的方法。你怎么看?它是否正确?