0

假设我有 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 都是正确的方法。你怎么看?它是否正确?

4

2 回答 2

1

这个 railscasts 将回答这个问题:

http://railscasts.com/episodes/154-polymorphic-association?autoplay=true

于 2012-07-26T02:52:14.857 回答
0

好的,所以我最终建立了一个关联,您可以从这里这里has_many :through获得更多帮助。

于 2012-07-28T15:47:43.760 回答